Image.FromFile 方法

定义

从指定的文件创建 ImageCreates an Image from the specified file.

重载

FromFile(String)

从指定的文件创建 ImageCreates an Image from the specified file.

FromFile(String, Boolean)

使用该文件中的嵌入颜色管理信息,从指定的文件创建 ImageCreates an Image from the specified file using embedded color management information in that file.

FromFile(String)

从指定的文件创建 ImageCreates an Image from the specified file.

public:
 static System::Drawing::Image ^ FromFile(System::String ^ filename);
public static System.Drawing.Image FromFile (string filename);
static member FromFile : string -> System.Drawing.Image
Public Shared Function FromFile (filename As String) As Image

参数

filename
String

字符串,包含要从中创建 Image 的文件的名称。A string that contains the name of the file from which to create the Image.

返回

此方法创建的 ImageThe Image this method creates.

异常

该文件没有有效的图像格式。The file does not have a valid image format.

-or- GDI+GDI+ 不支持文件的像素格式。does not support the pixel format of the file.

指定的文件不存在。The specified file does not exist.

filenameUrifilename is a Uri.

示例

下面的代码示例演示如何使用FromFile GetPropertyItemSetPropertyItem方法。The following code example demonstrates how to use the FromFileGetPropertyItem and SetPropertyItem methods. 此示例旨在与 Windows 窗体一起使用。This example is designed to be used with Windows Forms. 若要运行此示例,请将其粘贴到窗体中,并Paint通过DemonstratePropertyItem调用方法传递e作为PaintEventArgs来处理窗体的事件。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 );
   }
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);
}
Private Sub DemonstratePropertyItem(ByVal e As PaintEventArgs)

    ' Create two images.
    Dim image1 As Image = Image.FromFile("c:\FakePhoto1.jpg")
    Dim image2 As Image = Image.FromFile("c:\FakePhoto2.jpg")

    ' Get a PropertyItem from image1.
    Dim propItem As PropertyItem = 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)
End Sub

注解

托管 GDI + 具有支持以下文件类型的内置编码器和解码器:Managed GDI+ has built-in encoders and decoders that support the following file types:

  • BMPBMP

  • GIFGIF

  • JPEGJPEG

  • PNGPNG

  • TIFFTIFF

在释放之前Image ,该文件将保持锁定状态。The file remains locked until the Image is disposed.

如果文件没有有效的图像格式,或者如果GDI+GDI+不支持文件的像素格式,则此方法将OutOfMemoryException引发异常。If the file does not have a valid image format or if GDI+GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.

备注

Image不支持位图中的 alpha 透明度。The Image class does not support alpha transparency in bitmaps. 若要启用 alpha 透明度,请使用每个像素32位的 PNG 图像。To enable alpha transparency, use PNG images with 32 bits per pixel.

另请参阅

FromFile(String, Boolean)

使用该文件中的嵌入颜色管理信息,从指定的文件创建 ImageCreates an Image from the specified file using embedded color management information in that file.

public:
 static System::Drawing::Image ^ FromFile(System::String ^ filename, bool useEmbeddedColorManagement);
public static System.Drawing.Image FromFile (string filename, bool useEmbeddedColorManagement);
static member FromFile : string * bool -> System.Drawing.Image
Public Shared Function FromFile (filename As String, useEmbeddedColorManagement As Boolean) As Image

参数

filename
String

字符串,包含要从中创建 Image 的文件的名称。A string that contains the name of the file from which to create the Image.

useEmbeddedColorManagement
Boolean

若要使用图像文件中嵌入的颜色管理信息,则设置为 true;否则设置为 falseSet to true to use color management information embedded in the image file; otherwise, false.

返回

此方法创建的 ImageThe Image this method creates.

异常

该文件没有有效的图像格式。The file does not have a valid image format.

-or- GDI+GDI+ 不支持文件的像素格式。does not support the pixel format of the file.

指定的文件不存在。The specified file does not exist.

filenameUrifilename is a Uri.

示例

下面的代码示例演示如何使用FromFile方法获取新位图。The following code example demonstrates how to obtain a new bitmap using the FromFile method. 它还演示了TextureBrushIt also demonstrates a TextureBrush.

此示例旨在与 Windows 窗体一起使用。This example is designed to be used with Windows Forms. 创建一个包含名为Button2的按钮的窗体。Create a form containing a button named Button2. 将代码粘贴到窗体中,并Button2_Click将该方法与按钮Click的事件关联。Paste the code into the form and associate the Button2_Click method with the button's Click event.

private:
   void Button2_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      try
      {
         Bitmap^ image1 = dynamic_cast<Bitmap^>(Image::FromFile( "C:\\Documents and Settings\\"
         "All Users\\Documents\\My Music\\music.bmp", true ));
         TextureBrush^ texture = gcnew TextureBrush( image1 );
         texture->WrapMode = System::Drawing::Drawing2D::WrapMode::Tile;
         Graphics^ formGraphics = this->CreateGraphics();
         formGraphics->FillEllipse( texture, RectangleF(90.0F,110.0F,100,100) );
         delete formGraphics;
      }
      catch ( System::IO::FileNotFoundException^ ) 
      {
         MessageBox::Show( "There was an error opening the bitmap."
         "Please check the path." );
      }
   }
private void Button2_Click(System.Object sender, System.EventArgs e)
{
    try
    {
        Bitmap image1 = (Bitmap) Image.FromFile(@"C:\Documents and Settings\" +
            @"All Users\Documents\My Music\music.bmp", true);

        TextureBrush texture = new TextureBrush(image1);
        texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
        Graphics formGraphics = this.CreateGraphics();
        formGraphics.FillEllipse(texture, 
            new RectangleF(90.0F, 110.0F, 100, 100));
        formGraphics.Dispose();

    }
    catch(System.IO.FileNotFoundException)
    {
        MessageBox.Show("There was an error opening the bitmap." +
            "Please check the path.");
    }

}
Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    Try
        Dim image1 As Bitmap = _
            CType(Image.FromFile("C:\Documents and Settings\" _
            & "All Users\Documents\My Music\music.bmp", True), Bitmap)

        Dim texture As New TextureBrush(image1)
        texture.WrapMode = Drawing2D.WrapMode.Tile
        Dim formGraphics As Graphics = Me.CreateGraphics()
        formGraphics.FillEllipse(texture, _
            New RectangleF(90.0F, 110.0F, 100, 100))
        formGraphics.Dispose()

    Catch ex As System.IO.FileNotFoundException
        MessageBox.Show("There was an error opening the bitmap." _
            & "Please check the path.")
    End Try

End Sub

注解

托管 GDI + 具有支持以下文件类型的内置编码器和解码器:Managed GDI+ has built-in encoders and decoders that support the following file types:

  • BMPBMP

  • GIFGIF

  • JPEGJPEG

  • PNGPNG

  • TIFFTIFF

如果文件没有有效的图像格式,或者如果GDI+GDI+不支持文件的像素格式,则此方法将OutOfMemoryException引发异常。If the file does not have a valid image format or if GDI+GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.

在释放之前Image ,该文件将保持锁定状态。The file remains locked until the Image is disposed.

参数指定新Image应用的颜色更正是否根据图像文件中嵌入的颜色管理信息而进行。 useEmbeddedColorManagementThe useEmbeddedColorManagement parameter specifies whether the new Image applies color correction according to color management information that is embedded in the image file. 嵌入的信息可包括国际颜色联合会(ICC)配置文件、伽玛值和 chromaticity 信息。Embedded information can include International Color Consortium (ICC) profiles, gamma values, and chromaticity information.

备注

Image不支持位图中的 alpha 透明度。The Image class does not support alpha transparency in bitmaps. 若要启用 alpha 透明度,请使用每个像素32位的 PNG 图像。To enable alpha transparency, use PNG images with 32 bits per pixel.

另请参阅

适用于