图像与位图类

发布于 2024-07-23 07:08:28 字数 112 浏览 2 评论 0原文

我无法理解 Image 类和 Bitmap 类之间的差异。 现在,我知道 Bitmap 继承自 Image,但据我了解,两者非常相似。 有人能解释一下吗?

I have trouble understanding the differences between the Image class and the Bitmap class. Now, I know that the Bitmap inherits from the Image but from what I understand both are very similar. Can anyone shed some light on this please?

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

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

发布评论

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

评论(3

热鲨 2024-07-30 07:08:28

Bitmap 类是Image 类的实现。 Image类是一个抽象类;

Bitmap 类包含 12 个构造函数,它们通过不同的参数构造 Bitmap 对象。 它可以从另一个位图和图像的字符串地址构造位图。

请参阅此综合示例了解更多内容。

The Bitmap class is an implementation of the Image class. The Image class is an abstract class;

The Bitmap class contains 12 constructors that construct the Bitmap object from different parameters. It can construct the Bitmap from another bitmap, and the string address of the image.

See more in this comprehensive sample.

零度° 2024-07-30 07:08:28

这是一个澄清,因为我看到代码中所做的事情确实令人困惑 - 我认为以下示例可能对其他人有帮助。

正如其他人之前所说 - Bitmap 继承自抽象 Image

Abstract 实际上意味着您无法创建它的 New() 实例。

    Image imgBad1 = new Image();        // Bad - won't compile
    Image imgBad2 = new Image(200,200); // Bad - won't compile

但是您可以执行以下操作:

    Image imgGood;  // Not instantiated object!
    // Now you can do this
    imgGood = new Bitmap(200, 200);

如果执行了以下操作,您现在可以像使用同一个位图对象一样使用 imgGood:

    Bitmap bmpGood = new Bitmap(200,200);

这里的好处是您可以使用 Graphics 绘制 imgGood 对象 object

    Graphics gr = default(Graphics);
    gr = Graphics.FromImage(new Bitmap(1000, 1000));
    Rectangle rect = new Rectangle(50, 50, imgGood.Width, imgGood.Height); // where to draw
    gr.DrawImage(imgGood, rect);

这里imgGood可以是任何图像对象 - 位图、图元文件或任何其他继承自图像的对象!

This is a clarification because I have seen things done in code which are honestly confusing - I think the following example might assist others.

As others have said before - Bitmap inherits from the Abstract Image class

Abstract effectively means you cannot create a New() instance of it.

    Image imgBad1 = new Image();        // Bad - won't compile
    Image imgBad2 = new Image(200,200); // Bad - won't compile

But you can do the following:

    Image imgGood;  // Not instantiated object!
    // Now you can do this
    imgGood = new Bitmap(200, 200);

You can now use imgGood as you would the same bitmap object if you had done the following:

    Bitmap bmpGood = new Bitmap(200,200);

The nice thing here is you can draw the imgGood object using a Graphics object

    Graphics gr = default(Graphics);
    gr = Graphics.FromImage(new Bitmap(1000, 1000));
    Rectangle rect = new Rectangle(50, 50, imgGood.Width, imgGood.Height); // where to draw
    gr.DrawImage(imgGood, rect);

Here imgGood can be any Image object - Bitmap, Metafile, or anything else that inherits from Image!

瑾兮 2024-07-30 07:08:28

Image 提供了对任意图像的抽象访问,它定义了一组可以逻辑地应用于 Image 的任何实现的方法。 它不受任何特定图像格式或实现的限制。 Bitmap是图像抽象类的具体实现,它封装了Windows GDI位图对象。 位图只是 Image 抽象类的一个特定实现,它依赖于 GDI 位图对象。

例如,您可以通过继承 Image 类并实现抽象方法来创建自己的 Image 抽象实现。

不管怎样,这只是 OOP 的一个简单的基本用法,应该不难理解。

Image provides an abstract access to an arbitrary image , it defines a set of methods that can loggically be applied upon any implementation of Image. Its not bounded to any particular image format or implementation . Bitmap is a specific implementation to the image abstract class which encapsulate windows GDI bitmap object. Bitmap is just a specific implementation to the Image abstract class which relay on the GDI bitmap Object.

You could for example , Create your own implementation to the Image abstract , by inheriting from the Image class and implementing the abstract methods.

Anyway , this is just a simple basic use of OOP , it shouldn't be hard to catch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文