Image.FromFile 方法
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從指定的檔案建立 Image 物件。
FromFile(String) |
從指定的檔案建立 Image 物件。 |
FromFile(String, Boolean) |
使用指定之檔案中的內嵌色彩管理資訊,從該檔案建立 Image。 |
- 來源:
- 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
參數
傳回
這個方法建立的 Image。
例外狀況
指定的檔案不存在。
filename
為 Uri。
範例
下列程式代碼範例示範如何使用 FromFileGetPropertyItem 和 SetPropertyItem 方法。 此範例的設計目的是要與 Windows Forms 搭配使用。 若要執行此範例,請將它貼到表單中,並藉由呼叫 DemonstratePropertyItem
方法來處理表單的事件Paint,並傳遞e
為 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 擲回例外狀況。
注意
類別 Image 不支援位圖中的Alpha透明度。 若要啟用 Alpha 透明度,請使用每個圖元 32 位的 PNG 影像。
另請參閱
適用於
.NET Framework 4.8.1 及其他版本
產品 | 版本 |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |
- 來源:
- 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
參數
- useEmbeddedColorManagement
- Boolean
若要使用內嵌在影像檔的色彩管理資訊,則設定為 true
,否則為 false
。
傳回
這個方法建立的 Image。
例外狀況
指定的檔案不存在。
filename
為 Uri。
範例
下列程式代碼範例示範如何使用 方法來取得新的點陣圖 FromFile 。 它也會示範 TextureBrush。
此範例的設計目的是要與 Windows Forms 搭配使用。 Create 表單,其中包含名為Button2
的按鈕。 將程式代碼貼到表單中,並將方法與按鈕的事件Click產生關聯Button2_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 是否套用色彩校正。 內嵌資訊可能包括國際色彩聯盟 () 配置檔、gamma 值和色度資訊。
注意
類別 Image 不支援位圖中的Alpha透明度。 若要啟用 Alpha 透明度,請使用每個圖元 32 位的 PNG 影像。
另請參閱
適用於
.NET Framework 4.8.1 及其他版本
產品 | 版本 |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |