问题描述
在Form1中,我在构造函数中创建一个新的位图:
In Form1 I create in the constructor a new Bitmap:
public Form1()
{
InitializeComponent();
de.pb1 = pictureBox1;
de.bmpWithPoints = new Bitmap(pictureBox1.Width, pictureBox1.Height);
de.numberOfPoints = 100;
de.randomPointsColors = false;
de.Init();
}
在课堂上,我检查位图是否为空:
In the class I check if the bitmap is null :
if (bmpWithPoints == null)
位图不为null,但也未绘制任何内容.我检查该类是否为空,我想在位图上绘制并设置点.
The bitmap is not null but is also not drawn with anything on it.I check in the class if it's null I want to draw and set points on the bitmap.
if (bmpWithPoints == null)
{
for (int x = 0; x < bmpWithPoints.Width; x++)
{
for (int y = 0; y < bmpWithPoints.Height; y++)
{
bmpWithPoints.SetPixel(x, y, Color.Black);
}
}
Color c = Color.Red;
for (int x = 0; x < numberOfPoints; x++)
{
for (int y = 0; y < numberOfPoints; y++)
{
if (randomPointsColors == true)
{
c = Color.FromArgb(
r.Next(0, 256),
r.Next(0, 256),
r.Next(0, 256));
}
else
{
c = pointsColor;
}
bmpWithPoints.SetPixel(r.Next(0, bmpWithPoints.Width),
r.Next(0, bmpWithPoints.Height), c);
}
}
}
else
{
randomPointsColors = false;
}
也许问题不应该是图像为空或为空,我不确定如何调用它.也许只是一个新形象.但是我想检查一下,如果新的位图是(空的)没有在其上绘制任何东西,然后设置像素(点).
Maybe the question should not be if the image is empty or null, I'm not sure how to call it. Maybe just a new image. But i want to check that if the new bitmap is (empty) nothing drawn on it then set the pixels(points).
推荐答案
您可以创建一种检查图像像素的方法.作为一种选择,您可以使用 LockBits
方法将位图字节转换为字节数组并使用它们:
You can create a method which checks image pixels. As an option, you can use LockBits
method to get bitmap bytes into a byte array and use them:
bool IsEmpty(Bitmap image)
{
var data = image.LockBits(new Rectangle(0,0, image.Width,image.Height),
ImageLockMode.ReadOnly, image.PixelFormat);
var bytes = new byte[data.Height * data.Stride];
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
image.UnlockBits(data);
return bytes.All(x => x == 0);
}
这篇关于如何检查位图图像是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!