Image.FromFile 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从指定文件创建一个 Image 。
重载
| 名称 | 说明 |
|---|---|
| FromFile(String) |
从指定文件创建一个 Image 。 |
| FromFile(String, Boolean) |
使用该文件中的嵌入颜色管理信息从指定文件创建一个 Image 。 |
FromFile(String)
从指定文件创建一个 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 窗体一起使用。 若要运行此示例,请将其粘贴到窗体中,并通过调用Paint该方法(作为传递DemonstratePropertyItem方式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
注解
托管 GDI+ 具有支持以下文件类型的内置编码器和解码器:
BMP
GIF
JPEG
PNG
TIFF
文件保持锁定状态, Image 直到释放。
如果文件没有有效的图像格式,或者 GDI+ 不支持文件的像素格式,此方法将引发异常 OutOfMemoryException 。
Note
该 Image 类不支持位图中的 alpha 透明度。 若要启用 alpha 透明度,请使用每个像素 32 位的 PNG 图像。
另请参阅
适用于
FromFile(String, Boolean)
使用该文件中的嵌入颜色管理信息从指定文件创建一个 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 窗体一起使用。 创建包含名为 的 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)配置文件、伽玛值和色度信息。
Note
该 Image 类不支持位图中的 alpha 透明度。 若要启用 alpha 透明度,请使用每个像素 32 位的 PNG 图像。