标签:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void LockUnlockBitsExample(PaintEventArgs e) { //Create a new bitmap Bitmap bmp = new Bitmap("house.jpg"); //Lock the bitmap‘s bits Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); //Get the address of the fist line IntPtr ptr = bmpData.Scan0; //Declare an array to hold the bytes of the bitmap int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; //Copy the RGB values into the array Marshal.Copy(ptr, rgbValues, 0, bytes); //Set every third value to 255. A 24bpp bitmap will look red //bitmap图像是BGR的方式存储的。 for (int counter = 1; counter < rgbValues.Length; counter += 3) { rgbValues[counter] = 255; } //Copy the RGB values back to the bitmap Marshal.Copy(rgbValues, 0, ptr, bytes); //Unlock the bits bmp.UnlockBits(bmpData); //Draw the modified image e.Graphics.DrawImage(bmp, 0, 150); } private void Form1_Paint(object sender, PaintEventArgs e) { LockUnlockBitsExample(e); } } }
标签:
原文地址:http://www.cnblogs.com/stemon/p/4257450.html