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

上图片为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);


        }
原文地址:https://www.cnblogs.com/wangjixianyun/p/2835383.html