Difference Between Bitmap. Fromfile(Path) and New Bitmap(Path)

I woud like to know the difference between these two:

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

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

1

3 Answers

The 'FromFile' method comes from the abstract base class Image, which returns an Image object. Whereas the Bitmap class inherits the Image class, and the Bitmap constructor allows you to initialize Bitmap object directly.

In your second line of code, what you are trying to do is call FromFile() and get an Image object, and then type cast it to Bitmap. There's not really a good reason to do this manually, when the bitmap constructor can do it for you.

Both methods get a handle to the image via the path argument. Image.FromFile will return the superclass Image, while the former will simply return the Bitmap so you can avoid the cast.

Internally, they pretty much do the same:

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);
}

Hard to say - internally both method are very close, except that Image.FromFile() will check if the file exists and throw a FileNotFoundException if this is not the case.

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

These Gdip-methods lead to two MSDN articles (Bitmap.ctor() & Image.FromFile()) which are pretty close to each other, however the supported file formats seem to differ:

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

Anyway, if you know that you will have Bitmap, I'd prefer new Bitmap("C:\\test.bmp") just to get rid of the need to cast the image afterwards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest