19

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 ?

Kas
  • 3,747
  • 5
  • 29
  • 56

5 Answers5

18

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.

Andy
  • 3,997
  • 2
  • 19
  • 39
  • 6
    If I might add -- don't be afraid to pass a variable of type `Bitmap` to a function that takes `Image` as a parameter... since `Bitmap` is an `Image`, it will accept it. For example, `g.DrawImage(mybitmap, 0, 0)` is valid with `mybitmap` of type `Bitmap` (here, `g` is of type `Graphics`, and `DrawImage` takes `Image` as a parameter). – BeemerGuy Oct 30 '13 at 09:48
  • @CPerkins You are right. Thanks for pointing this out! I adjusted my post, feel free to improve it. – Andy May 28 '19 at 19:41
  • Is there a benefit of using, for example, `Image.FromFile()` directly? I have seen it used many times to only be cast to bitmap in the next step. – Adrian Hood Sr Jul 19 '21 at 16:20
  • From the source: https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Image.cs,e328c776c1ca78db,references It seems it could also return a `Metafile` object. Then a cast to `Bitmap` would fail. But you usually know beforehand which type of object is expected. – Andy Jul 19 '21 at 17:16
4

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.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
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.

Jensen
  • 3,498
  • 2
  • 26
  • 43
1

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

Arul Dinesh
  • 550
  • 4
  • 15
0
Bitmap bmp = (Bitmap)Image.FromFile(path); // Works

So, although you can't create an object of type "Image", you can still use its methods. Note however the cast to Bitmap.

Guese
  • 1
  • 1