您的位置:首页 > 编程语言 > C#

c#载入图片,修改图片的二进制数据

2012-12-27 11:28 288 查看


上图片为rar合并图片,图片另存为后后缀改为rar,可以解压缩出项目。

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Bitmap bitmap = new Bitmap("p.png");//如果用png图片,格式是rgb,如果用大小如240,240,格式是rgba
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, 200, 200), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr start = data.Scan0;

// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(data.Stride) * bitmap.Height;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(start, rgbValues, 0, bytes);

// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255;

// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, start, bytes);

// Unlock the bits.
bitmap.UnlockBits(data);

g.DrawImage(bitmap, 0, 0, 200, 200);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: