0

So that I can bind a picturebox to a datasource in winforms I have created a property that returns a tiny bitmap if the data is null.

I need a function like

private static bool IsBlankImage(Image img)
{
   return (img ==  new Bitmap(1, 1);
}

However this always returns false. What am I doing wrong?

Further explanation of the technique I need the function for is outlined my answer to the question here

Community
  • 1
  • 1
Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

1

Your method can never return true since it is checking for reference equality with newly created bitmap. Obviously they are different references.

However, you can implement some kind of value equality like this.

private static bool IsBlankImage(Image img)
{
    Bitmap bmp = img as Bitmap;
    if (bmp == null)
    {
        return false;
    }
    return bmp.Size == new Size(1, 1) &&
        bmp.GetPixel(0, 0).ToArgb() == 0;
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189