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)
?
Bitmap is short for BMP is an image file format that can be used to create and store computer graphics.
Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) Initializes a new instance of the Bitmap class with the specified size, pixel format, and pixel data.
A Bitmap is an object used to work with images defined by pixel data.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With