Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 427
Matias Cicero Avatar asked Aug 25 '15 14:08

Matias Cicero


People also ask

What is Bitmap c#?

Bitmap is short for BMP is an image file format that can be used to create and store computer graphics.

Which class is used to create Bitmap?

Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) Initializes a new instance of the Bitmap class with the specified size, pixel format, and pixel data.

What is Bitmap objects?

A Bitmap is an object used to work with images defined by pixel data.

What is the difference between bitmap and JPEG?

BMP and JPEG are two of the oldest image file types, dating back to the early 1990s. BMP files contain large, raw, high-quality images, which makes them better for editing. JPEG files automatically compress, so they're generally smaller and of lower quality.


2 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.

like image 139
Binod Mahto Avatar answered Oct 13 '22 00:10

Binod Mahto


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);
}
like image 32
Yuval Itzchakov Avatar answered Oct 13 '22 01:10

Yuval Itzchakov