Difference between Bitmap.FromFile (path) and new Bitmap (path)

I wanted to know the difference between the two:

Bitmap bitmap1 = new Bitmap("C:\\test.bmp"); Bitmap bitmap2 = (Bitmap) Bitmap.FromFile("C:\\test.bmp"); 

Is one option better than the other? Does Bitmap.FromFile(path) any additional data in the bitmap or is it just a delegate for new Bitmap(path) ?

+5
source share
3 answers

Both methods get an image descriptor via the path argument. Image.FromFile will return the Image superclass, and the first will just return the Bitmap so you can avoid the cast.

Inside, they pretty much do the same thing:

 public static Image FromFile(String filename, bool useEmbeddedColorManagement) { if (!File.Exists(filename)) { IntSecurity.DemandReadFileIO(filename); throw new FileNotFoundException(filename); } filename = Path.GetFullPath(filename); IntPtr image = IntPtr.Zero; int status; if (useEmbeddedColorManagement) { status = SafeNativeMethods.Gdip.GdipLoadImageFromFileICM(filename, out image); } else { status = SafeNativeMethods.Gdip.GdipLoadImageFromFile(filename, out image); } if (status != SafeNativeMethods.Gdip.Ok) throw SafeNativeMethods.Gdip.StatusException(status); status = SafeNativeMethods.Gdip.GdipImageForceValidation(new HandleRef(null, image)); if (status != SafeNativeMethods.Gdip.Ok) { SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(null, image)); throw SafeNativeMethods.Gdip.StatusException(status); } Image img = CreateImageObject(image); EnsureSave(img, filename, null); return img; } 

and

 public Bitmap(String filename) { IntSecurity.DemandReadFileIO(filename); filename = Path.GetFullPath(filename); IntPtr bitmap = IntPtr.Zero; int status = SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(filename, out bitmap); if (status != SafeNativeMethods.Gdip.Ok) throw SafeNativeMethods.Gdip.StatusException(status); status = SafeNativeMethods.Gdip.GdipImageForceValidation(new HandleRef(null, bitmap)); if (status != SafeNativeMethods.Gdip.Ok) { SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(null, bitmap)); throw SafeNativeMethods.Gdip.StatusException(status); } SetNativeImage(bitmap); EnsureSave(this, filename, null); } 
+1
source

It is hard to say that internally both methods are very close, except that Image.FromFile() checks if the file exists and throws a FileNotFoundException if it is not.

The main difference is that Bitmap.ctor() calls GdipCreateBitmapFromFile internally, and Image.FromFile() calls GdipLoadImageFromFile ;

These Gdip methods lead to two MSDN articles ( Bitmap.ctor () and Image.FromFile () ), which are pretty close to each other, but the supported file formats look different:

 Bitmap: BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF Image: BMP, GIF, JPEG, PNG, TIFF, and EMF. 

In any case, if you know that you will have a bitmap, I would prefer new Bitmap("C:\\test.bmp") just get rid of the need to take a picture later.

+1
source

"FromFile" is accessible through the base class "Image" (which is an abstract class) into the class "Bitmap" and returns an Image object. Where "Bitmap" is a child class that inherits the "Image" class and the Bitmap constructor, it allows you to directly initialize a Bitmap object.

In your case, what are you trying to do, call the FromFile method and get an Image object, and then enter it in Bitmap. Why do this if there is a Bitmap constructor for this. Does Bitmap.FromFile (path) populate any additional data for the bitmap: No

+1
source

All Articles