Image.FromFile 方法

定義

從指定的檔案建立一個 Image

多載

名稱 Description
FromFile(String)

從指定的檔案建立一個 Image

FromFile(String, Boolean)

利用該檔案中嵌入的色彩管理資訊,從指定檔案建立 。Image

FromFile(String)

來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
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

範例

以下程式碼範例示範如何使用 FromFileGetPropertyItem and SetPropertyItem 方法。 此範例設計用於 Windows 表單。 執行此範例時,將它貼入表單,並呼叫Paint該表DemonstratePropertyIteme事件,傳遞為 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

備註

Managed GDI+ 內建編碼器與解碼器,支援以下檔案類型:

  • BMP

  • GIF

  • JPEG

  • PNG

  • TIFF

檔案會一直鎖定,直到被 Image 處理掉為止。

若檔案沒有有效的影像格式,或 GDI+ 不支援該檔案的像素格式,此方法會拋 OutOfMemoryException 出例外。

Note

這個 Image 類別不支援點陣圖中的 alpha 透明度。 要啟用 alpha 透明,請使用 32 位元每像素的 PNG 影像。

另請參閱

適用於

FromFile(String, Boolean)

來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
Image.cs
來源:
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

備註

Managed GDI+ 內建編碼器與解碼器,支援以下檔案類型:

  • BMP

  • GIF

  • JPEG

  • PNG

  • TIFF

若檔案沒有有效的影像格式,或 GDI+ 不支援該檔案的像素格式,此方法會拋 OutOfMemoryException 出例外。

檔案會一直鎖定,直到被 Image 處理掉為止。

useEmbeddedColorManagement 參數指定新版本 Image 是否根據嵌入影像檔案中的色彩管理資訊來套用色彩校正。 內嵌資訊可能包括國際色彩聯盟(ICC)的剖面、伽瑪值及色度資訊。

Note

這個 Image 類別不支援點陣圖中的 alpha 透明度。 要啟用 alpha 透明,請使用 32 位元每像素的 PNG 影像。

另請參閱

適用於