有没有办法检查 .NET 中的 Bitmap 是否为空?

发布于 2024-12-15 15:08:18 字数 179 浏览 6 评论 0原文

我正在尝试检查位图对象以查看它是否已设置或为空。 .NET好像没有这个功能。我查看了 MSDN 库站点和 stackoverflow,很少提及与 .NET 相关的内容。在.NET 中还有其他方法可以做到这一点吗?

当 TBitmaap 不包含任何图像时,其 Empty 属性设置为 True

任何帮助将不胜感激。

I am trying to check on a bitmap object to see if it either set or empty. It seems .NET doesn't have that function. I've looked in MSDN library site and stackoverflow and there is very little mention of this in relation to .NET. Is there any other way to do that in .NET?

When TBitmaap doesn't contain any image its Empty property is set to True

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓味的萝莉 2024-12-22 15:08:18

您对位图的唯一选择是它被实例化或为空,并且从阅读评论和您的答案来看,您正在尝试做的事情令人困惑。

您实际上只需要检查位图是否为空,我认为这相当于您所说的语言是空的:

private Bitmap _bmp;

private void button1_Click(object sender, EventArgs e) {
  if (_bmp == null)
    _bmp = new Bitmap(@"c:\example.bmp");
}

您可以对其进行扩展,如下所示:

public static class MyExensions {
  public static bool IsEmpty(this Bitmap bitmap) {
    return (bitmap == null);
  }
}

这会将您的代码变成这:

private void button1_Click(object sender, EventArgs e) {
  if (_bmp.IsEmpty())
    _bmp = new Bitmap(@"c:\example.bmp");
}

Your only options for a bitmap is that it is instantiated or its null, and from reading the comments and your answer, it's confusing what you are trying to do.

You really just need to check if the bitmap is null or not, which is, I think, equivalent to the language you are saying, is empty:

private Bitmap _bmp;

private void button1_Click(object sender, EventArgs e) {
  if (_bmp == null)
    _bmp = new Bitmap(@"c:\example.bmp");
}

You can make an extension out of it, like this:

public static class MyExensions {
  public static bool IsEmpty(this Bitmap bitmap) {
    return (bitmap == null);
  }
}

and that would turn your code into this:

private void button1_Click(object sender, EventArgs e) {
  if (_bmp.IsEmpty())
    _bmp = new Bitmap(@"c:\example.bmp");
}
握住我的手 2024-12-22 15:08:18

如果我错了请纠正我。

来自 Delphi win32,我知道您可以创建位图对象并稍后设置其图像属性,如下所示。

Bitmap:TBitmap;
Bitmap := TBitmap.Create;
Bitmap.LoadFromFile('c:\example.bmp');

在这种情况下,您不能只检查 Bitmap 对象是否为 null 或 nil。您需要实际检查图像属性是否已设置或为空。

就.NET而言,当您创建位图对象时,您必须将图像作为参数传递给其构造函数。这意味着构造函数实例化并设置其图像。您可以检查图像分辨率或宽度和高度是否已设置,如 Henk Holterman 指出的那样。

    image1 = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Music\music.bmp", true);

Correct me if I am wrong.

Coming from Delphi win32, I know you can create an object of a bitmap and set its image property later as follows.

Bitmap:TBitmap;
Bitmap := TBitmap.Create;
Bitmap.LoadFromFile('c:\example.bmp');

In that case, you can't just check to see if Bitmap object is null or nil. You need to actually check if the image property is set or empty.

As far as .NET, when you create an object of a bitmap, you have to pass in the image as a parameter to its constructor. That means constructor instantiates and sets its image. You could check if the image resolution or width and height is set or not like Henk Holterman pointed out.

    image1 = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Music\music.bmp", true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文