Bitmap.GetPixel () returns a bad value

I'm having trouble reading the pixel values ​​from the bitmap that I am generating. First I create a bitmap named maskBitmap in my class using this code:

void generateMaskBitmap() { if (inputBitmap != null) { Bitmap tempBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(tempBitmap)) { Brush brush = Brushes.Black; for (int y = 0; y < tempBitmap.Height; y += circleSpacing) { for (int x = 0; x < tempBitmap.Width; x += circleSpacing) { g.FillEllipse(brush, x, y, circleDiameter, circleDiameter); } } g.Flush(); } maskBitmap = (Bitmap)tempBitmap.Clone(); } } 

Then I will try to apply the mask to the source image using the following code:

  void generateOutputBitmap() { if (inputBitmap != null && maskBitmap != null) { Bitmap tempBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height); for (int y = 0; y < tempBitmap.Height; y++) { for (int x = 0; x < tempBitmap.Width; x++) { Color tempColor = maskBitmap.GetPixel(x, y); if (tempColor == Color.Black) { tempBitmap.SetPixel(x, y, inputBitmap.GetPixel(x, y)); } else { tempBitmap.SetPixel(x, y, Color.White); } } } outputBitmap = tempBitmap; } } 

The bitmap image of the mask is successfully generated and displayed in the image window, however, the color values ​​for each pixel when testing with " tempColor " are displayed empty (A = 0, R = 0, G = 0, B = 0). I know performance issues with getpixel / setpixel, but this is not a problem for this project. I also know that "tempColor == Color.Black" not a valid test, but this is just the place for my comparison code.

+4
source share
1 answer

I can not reproduce your problem. I copied and pasted your code and made some changes to make it work for me. I can confirm that tempColor sometimes # FF000000.

I suspect you have mixed bitmap links somewhere. Are you really sure you never get any value other than # 00000000? Do your circleDiameter and circleSpacing have reasonable values? And most importantly: are you absolutely sure that you are reading from the correct bitmap?

Here is my version of your code that I know works:

 using System; using System.Drawing; namespace Test { class Program { static void Main() { var bitmap = GenerateMaskBitmap(100, 100); TestMaskBitmap(bitmap); } const int CircleDiameter = 10; const int CircleSpacing = 10; static Bitmap GenerateMaskBitmap(int width, int height) { Bitmap maskBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(maskBitmap)) { Brush brush = Brushes.Black; for (int y = 0; y < maskBitmap.Height; y += CircleSpacing) { for (int x = 0; x < maskBitmap.Width; x += CircleSpacing) { g.FillEllipse(brush, x, y, CircleDiameter, CircleDiameter); } } g.Flush(); } return maskBitmap; } static void TestMaskBitmap(Bitmap maskBitmap) { for (int y = 0; y < maskBitmap.Height; y++) { for (int x = 0; x < maskBitmap.Width; x++) { Color tempColor = maskBitmap.GetPixel(x, y); if (tempColor.ToArgb() != 0) throw new Exception("It works!"); } } } } } 
+2
source

All Articles