C# 图像与位图类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/946926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image vs Bitmap class
提问by user116864
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?
我无法理解Image
班级和Bitmap
班级之间的差异。现在,我知道Bitmap
继承自Image
但据我所知两者非常相似。任何人都可以对此有所了解吗?
采纳答案by Richard
The Bitmap class is an implementation of the Image class. The Image class is an abstract class;
Bitmap 类是 Image 类的实现。Image 类是一个抽象类;
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.
Bitmap 类包含 12 个构造函数,它们从不同的参数构造 Bitmap 对象。它可以从另一个位图和图像的字符串地址构造位图。
See more in this comprehensive sample.
在此综合示例中查看更多信息。
回答by user88637
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.
Image 提供对任意图像的抽象访问,它定义了一组方法,可以在逻辑上应用于 Image 的任何实现。它不受任何特定图像格式或实现的限制。Bitmap是封装windows GDI位图对象的图像抽象类的具体实现。位图只是图像抽象类的一个具体实现,它中继在 GDI 位图对象上。
You could for example , Create your own implementation to the Image abstract , by inheriting from the Image class and implementing the abstract methods.
例如,您可以通过从 Image 类继承并实现抽象方法来创建自己的 Image 抽象实现。
Anyway , this is just a simple basic use of OOP , it shouldn't be hard to catch.
无论如何,这只是 OOP 的一个简单的基本用法,应该不难掌握。
回答by David Carta
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 - Bitmapinherits from the Abstract Imageclass
正如其他人之前所说 - Bitmap继承自 Abstract Image类
Abstract effectively means you cannot create a New() instance of it.
Abstract 实际上意味着您不能创建它的 New() 实例。
Image imgBad1 = new Image(); // Bad - won't compile
Image imgBad2 = new Image(200,200); // Bad - won't compile
Butyou 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:
如果您已完成以下操作,您现在可以像使用相同的位图对象一样使用 imgGood:
Bitmap bmpGood = new Bitmap(200,200);
The nice thing here is you can draw the imgGood object using a Graphicsobject
这里的好处是您可以使用Graphics对象绘制 imgGood对象
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 imgGoodcan be anyImage object - Bitmap, Metafile, or anything else that inherits from Image!
这里imgGood可以是任何Image 对象 - Bitmap、Metafile 或任何其他继承自 Image 的东西!