Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 18-13 extracted from chapter Windows forms applications


Listing 18-12<     > Listing 18-14


This listing can be compiled with the command line:
csc.exe /unsafe /target:exe Example_18_13.cs
Errors: 0 Warnings: 0


Example_18_13.cs
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class Form1 : System.Windows.Forms.Form {
   public struct StructPixel {
      public byte R; public byte G; public byte B;
   }
   private void OnClick(object sender, System.EventArgs e) {
      using Graphics g = CreateGraphics() ) {
         Bitmap m_Bmp = new Bitmap"Lena.jpg" );
         g.DrawImage( m_Bmp, new Point(55) );
         // Wait one second...
         System.Threading.Thread.Sleep( 1000 );
         unsafe {
            int width = m_Bmp.Width;
            int height = m_Bmp.Height;
            BitmapData bmpData = m_Bmp.LockBits(
               new Rectangle00, width, height ),
               ImageLockMode.ReadWrite,
               m_Bmp.PixelFormat);
            StructPixel* pCurrent = null;
            StructPixel* pBmp = (StructPixel*) bmpData.Scan0;
            for int = 0; y < width; y++ ) {
               pCurrent = pBmp + y * height;
               for int = 0; x < height; x++ ) {
                  pCurrent->R = (byte) (255 - pCurrent->R);
                  pCurrent->G = (byte) (255 - pCurrent->G);
                  pCurrent->B = (byte) (255 - pCurrent->B);
                  pCurrent++;
               }
            }
            m_Bmp.UnlockBits( bmpData );
         }
         g.DrawImage( m_Bmp, new Point(55) );
      }
   }
   private System.Windows.Forms.Button button;
   private System.ComponentModel.Container components = null;
   public Form1() {
      InitializeComponent();
   }
   protected override void Dispose(bool disposing) {
      if (disposing) {
         if (components != null) {
            components.Dispose();
         }
      }
      base.Dispose(disposing);
   }
   private void InitializeComponent() {
      this.button = new System.Windows.Forms.Button();
      this.SuspendLayout();
      this.button.Location = new System.Drawing.Point(20816);
      this.button.Name = "Click";
      this.button.TabIndex = 0;
      this.button.Text = "Click";
      this.button.Click += new System.EventHandler(this.OnClick);
      this.AutoScaleDimensions = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                             this.button});
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);
   }
   [System.STAThread]
   static void Main() {
      System.Windows.Forms.Application.Run(new Form1());
   }
}	
Copyright Patrick Smacchia 2006 2007