What is the difference between System.Drawing.Image and System.Drawing.Bitmap?

半城伤御伤魂 提交于 2020-01-14 07:15:09

问题


I am confused what's the different between System.Drawing.Image and System.Drawing.Bitmap

Can someone explain the major difference between those two types ?

And Why to use System.Drawing.Bitmap instead of System.Drawing.Image ?


回答1:


Bitmap inherits from Image:

System.Drawing.Bitmap : System.Drawing.Image
{ }

Image is an abstract class, this means:

The abstract modifier indicates that the thing being modified has a missing or incomplete implementation.

Bitmap is a sealed class, this means:

When applied to a class, the sealed modifier prevents other classes from inheriting from it.

See the following:

Bitmap bmp = new Bitmap(filename); // Works
Image img = new Image(); // The compiler says: "Cannot access internal constructer 'Image' here.

This is because Image is not meant to be used this way. It just provides functionality for the Bitmap class.

Thus use Bitmap when dealing with pixelated images, like jpeg, png, bmp, etc.

If you expect no specific type of image in your method and the methods of Image are sufficient, use the more general Image as parameter type. This method will then accept other classes inheriting from Image as well, for example Metafile.




回答2:


Am not sure what you mean difference?

System.Drawing.Image is the base class for System.Drawing.Bitmap.

System.Drawing.Image is abstract class as well, so you can't create instance of it. You'll have to create instance of System.Drawing.Bitmap only.

Image.FromFile, Image.BlahBlah... returns you instance of Bitmap only.




回答3:


As the MSDN documentation clearly states about System.Drawing.Image:

An abstract base class that provides functionality for the Bitmap and Metafile descended classes.

So you cannot compare them. The System.Drawing.Bitmap class is a concrete implementation of the abstract System.Drawing.Image class.




回答4:


Image is a base abstract class representing a raster image. Bitmap is one implementation of this abstract class based on GDI+.



来源:https://stackoverflow.com/questions/19678195/what-is-the-difference-between-system-drawing-image-and-system-drawing-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!