Export (0) Print
Expand All

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
)

Parameters

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.

ExceptionCondition
OutOfMemoryException

The file does not have a valid image format.

-or-

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

FileNotFoundException

The specified file does not exist.

ArgumentException

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.

NoteNote

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 FromFile GetPropertyItem 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.
    image2.SetPropertyItem(propItem);

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft