Image.FromFile 方法

定义

从指定的文件创建 Image

重载

FromFile(String)

从指定的文件创建 Image

FromFile(String, Boolean)

使用该文件中的嵌入颜色管理信息,从指定的文件创建 Image

FromFile(String)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

从指定的文件创建 Image

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 的文件的名称。

返回

此方法创建的 Image

例外

该文件没有有效的图像格式。

- 或 -

GDI+ 不支持文件的像素格式。

指定的文件不存在。

filenameUri

示例

下面的代码示例演示如何使用 FromFileGetPropertyItemSetPropertyItem 方法。 此示例旨在与 Windows 窗体 一起使用。 若要运行此示例,请将其粘贴到窗体中,并通过调用 DemonstratePropertyItem 方法处理窗体的事件Paint,并将其ePaintEventArgs作为 传递。

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+ 具有支持以下文件类型的内置编码器和解码器:

  • BMP

  • GIF

  • JPEG

  • PNG

  • TIFF

在释放 之前, Image 文件将保持锁定状态。

如果文件没有有效的图像格式,或者 GDI+ 不支持文件的像素格式,此方法将 OutOfMemoryException 引发异常。

备注

Image 不支持位图中的 alpha 透明度。 若要启用 alpha 透明度,请使用每像素 32 位的 PNG 图像。

另请参阅

适用于

FromFile(String, Boolean)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

使用该文件中的嵌入颜色管理信息,从指定的文件创建 Image

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 的文件的名称。

useEmbeddedColorManagement
Boolean

若要使用图像文件中嵌入的颜色管理信息,则设置为 true;否则设置为 false

返回

此方法创建的 Image

例外

该文件没有有效的图像格式。

- 或 -

GDI+ 不支持文件的像素格式。

指定的文件不存在。

filenameUri

示例

下面的代码示例演示如何使用 FromFile 方法获取新位图。 它还演示 了 。TextureBrush

此示例旨在与 Windows 窗体 一起使用。 创建包含名为 的按钮的 Button2窗体。 将代码粘贴到窗体中,并将 Button2_Click 方法与按钮的事件 Click 相关联。

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+ 具有支持以下文件类型的内置编码器和解码器:

  • BMP

  • GIF

  • JPEG

  • PNG

  • TIFF

如果文件没有有效的图像格式,或者 GDI+ 不支持文件的像素格式,此方法将 OutOfMemoryException 引发异常。

在释放 之前, Image 文件将保持锁定状态。

参数 useEmbeddedColorManagement 指定新 Image 是否根据嵌入在图像文件中的颜色管理信息应用颜色更正。 嵌入的信息可以包括国际颜色联盟 (ICC) 配置文件、伽玛值和色度信息。

备注

Image 不支持位图中的 alpha 透明度。 若要启用 alpha 透明度,请使用每像素 32 位的 PNG 图像。

另请参阅

适用于