Image.FromFile Method (String)


Creates an Image from the specified file.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

public static Image FromFile(
	string filename


Type: System.String

A string that contains the name of the file from which to create the Image.

Return Value

Type: System.Drawing.Image

The Image this method creates.

Exception Condition

The file does not have a valid image format.


GDI+ does not support the pixel format of the file.


The specified file does not exist.


filename is a Uri.

Managed GDI+ has built-in encoders and decoders that support the following file types:

  • BMP

  • GIF

  • JPEG

  • PNG

  • TIFF

The file remains locked until the Image is disposed.

If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.


The Image class does not support alpha transparency in bitmaps. To enable alpha transparency, use PNG images with 32 bits per pixel.

The following code example demonstrates how to use the FromFileGetPropertyItem and SetPropertyItem methods. This example is designed to be used with Windows Forms. To run this example, paste it into a form, and handle the form's Paint event by calling the DemonstratePropertyItem method, passing e as PaintEventArgs.

private void DemonstratePropertyItem(PaintEventArgs e)

    // Create two images.
    Image image1 = Image.FromFile("c:\\FakePhoto1.jpg");
    Image image2 = Image.FromFile("c:\\FakePhoto2.jpg");

    // Get a PropertyItem from image1.
    PropertyItem propItem = image1.GetPropertyItem(20624);

    // Change the ID of the PropertyItem.
    propItem.Id = 20625;

    // Set the PropertyItem for image2.

    // Draw the image.
    e.Graphics.DrawImage(image2, 20.0F, 20.0F);

.NET Framework
Available since 1.1
Return to top