usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Drawing.Imaging;namespacewindowsformsapplication4{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) {OpenFileDialog Dig=NewOpenFileDialog (); if(Dig. ShowDialog () = =DialogResult.OK) pictureBox1.Image=Image.FromFile (Dig. FileName); } unsafe Private voidButton2_Click (Objectsender, EventArgs e) {Bitmap Bitmap= (Bitmap) pictureBox1.Image.Clone ();//create a copy to avoid manipulating the original imageRectangle rect =NewRectangle (0,0, Bitmap. Width, Bitmap. Height); BitmapData Bmpdata=bitmap. LockBits (Rect, Imagelockmode.readwrite,bitmap. PixelFormat); byte* pix = (byte*) Bmpdata. Scan0; if(Bitmap. pixelformat==pixelformat.format8bppindexed)//whether it is a grayscale image { for(inti =0; I < Bmpdata. Height; i++) { for(intj =0; J < Bmpdata. Width; J + +) {pix[0] = (byte)(255-pix[0]);//Inverse Colorpix++; } } } Else { for(inti =0; I < Bmpdata. height;i++ ) { for(intj =0; J < Bmpdata. width;j++) {pix[0] = (byte)(255-pix[0]);//Inverse Colorpix[1] = (byte)(255-pix[1]); pix[2] = (byte)(255-pix[2]); Pix= pix +3; }}} bitmap. Unlockbits (Bmpdata); Picturebox2.image=bitmap; } }}
"C # Learning Notes" picture pixel operation