Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte[] rgbValues;
- private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
- {
- paintWind.Image = Bitmap.FromFile(openFileDialog1.FileName);
- g = Graphics.FromImage(paintWind.Image);
- Bitmap bmp = paintWind.Image as Bitmap;
- // Lock the bitmap's bits.
- Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
- System.Drawing.Imaging.BitmapData bmpData =
- bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
- bmp.PixelFormat);
- // Get the address of the first line.
- IntPtr ptr = bmpData.Scan0;
- // Declare an array to hold the bytes of the bitmap.
- int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
- rgbValues = new byte[bytes];
- // Copy the RGB values into the array.
- System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
- // Set every third value to 255. A 24bpp bitmap will look red.
- for (int counter = 0; counter < rgbValues.Length; counter+=3)
- {
- //rgbValues[counter+1] = 255;
- }
- // Copy the RGB values back to the bitmap
- System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
- // Unlock the bits.
- bmp.UnlockBits(bmpData);
- int i = 0;
- for (int counter = 0; counter < rgbValues.Length; counter += 3)
- {
- bmp.SetPixel(i % bmp.Width, i / bmp.Width,
- Color.FromArgb(rgbValues[counter], rgbValues[counter + 1], rgbValues[counter + 2]));
- i++;
- }
- paintWind.Refresh();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement