【问题标题】:Compare an image with new BitMap(1,1)将图像与新的 BitMap(1,1) 进行比较
【发布时间】:2014-06-05 05:03:05
【问题描述】:
为了可以将图片框绑定到 winforms 中的数据源,我创建了一个属性,如果数据为空,则该属性返回一个微小的位图。
我需要一个类似
的函数
private static bool IsBlankImage(Image img)
{
return (img == new Bitmap(1, 1);
}
但是,这总是返回 false。我做错了什么?
我需要该功能的技术的进一步解释概述了我的答案to the question here
【问题讨论】:
标签:
image
winforms
bitmap
【解决方案1】:
您的方法永远不会返回 true,因为它正在检查与新创建的位图的引用是否相等。显然它们是不同的引用。
但是,您可以像这样实现某种值相等。
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;
}