Reputation: 883
I'm trying to use Graphics.DrawImage() to copy a portion of a bitmap; however, the copy is a blank image. Each pixel has a zero value. To debug the problem, I created a simple unit test demonstrating the issue.
Why are all the pixels 0 in the new bitmap?
[Test]
public void CopyImage()
{
// Create a dummy bitmap using a counter.
Bitmap bitmap = new Bitmap(3, 3);
int color = 0;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
bitmap.SetPixel(x, y, Color.FromArgb(color));
color++;
}
}
// Copy the bitmap
Bitmap newBitmap = new Bitmap(3, 3);
Graphics g = Graphics.FromImage(newBitmap);
g.DrawImage(bitmap, new Point(0, 0));
g.Save();
// Validate the bitmap was copied correctly.
// All pixels have an ARGB value of 0 so the
// asserts fail.
color = 0;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
Assert.AreEqual(color, newBitmap.GetPixel(x, y).ToArgb());
color++;
}
}
}
Upvotes: 0
Views: 965
Reputation: 558
I think the color value passed to Color.FromArgb(color) has to be bigger than 0,1,2 to be visible like this hexadecimal if using one parameter:
Color.FromArgb(0x78FF0000)
Else pass three values for red, green, blue between 0 and 255 and use values like 0, 100, 200 to see a difference:
Color.FromArgb(255, 0, 0)
Upvotes: 3