zoukankan      html  css  js  c++  java
  • C#Image.FromFile Method

    Image.FromFile Method

    Definition

    Namespace:

    System.Drawing

    Assemblies:

    System.Drawing.dll, System.Drawing.Common.dll

    Creates an Image from the specified file.

    Overloads

    FromFile(String)

    Creates an Image from the specified file.

    FromFile(String, Boolean)

    Creates an Image from the specified file using embedded color management information in that file.

    FromFile(String)

    Creates an Image from the specified file.

    C#Copy

    public static System.Drawing.Image FromFile (string filename);

    Parameters

    filename

    String

    A string that contains the name of the file from which to create the Image.

    Returns

    Image

    The Image this method creates.

    Exceptions

    OutOfMemoryException

    The file does not have a valid image format.

    -or-

    GDI+ does not support the pixel format of the file.

    FileNotFoundException

    The specified file does not exist.

    ArgumentException

    filename is a Uri.

    Examples

    The following code example demonstrates how to use the FromFileGetPropertyItem and SetPropertyItem methods. This example is designed to be used with Windows Forms. 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.

    C#Copy

    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);
    }
    

    Remarks

    Managed GDI+ has built-in encoders and decoders that support the following file types:

    • BMP

    • GIF

    • JPEG

    • PNG

    • TIFF

    The file remains locked until the Image is disposed.

    If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.

     Note

    The Image class does not support alpha transparency in bitmaps. To enable alpha transparency, use PNG images with 32 bits per pixel.

    See also

    Types of Bitmaps

    FromFile(String, Boolean)

    Creates an Image from the specified file using embedded color management information in that file.

    C#Copy

    public static System.Drawing.Image FromFile (string filename, bool useEmbeddedColorManagement);

    Parameters

    filename

    String

    A string that contains the name of the file from which to create the Image.

    useEmbeddedColorManagement

    Boolean

    Set to true to use color management information embedded in the image file; otherwise, false.

    Returns

    Image

    The Image this method creates.

    Exceptions

    OutOfMemoryException

    The file does not have a valid image format.

    -or-

    GDI+ does not support the pixel format of the file.

    FileNotFoundException

    The specified file does not exist.

    ArgumentException

    filename is a Uri.

    Examples

    The following code example demonstrates how to obtain a new bitmap using the FromFile method. It also demonstrates a TextureBrush.

    This example is designed to be used with Windows Forms. Create a form containing a button named Button2. Paste the code into the form and associate the Button2_Click method with the button's Click event.

    C#Copy

    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.");
        }
    
    }
    

    Remarks

    Managed GDI+ has built-in encoders and decoders that support the following file types:

    • BMP

    • GIF

    • JPEG

    • PNG

    • TIFF

    If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.

    The file remains locked until the Image is disposed.

    The useEmbeddedColorManagement parameter specifies whether the new Image applies color correction according to color management information that is embedded in the image file. Embedded information can include International Color Consortium (ICC) profiles, gamma values, and chromaticity information.

     Note

    The Image class does not support alpha transparency in bitmaps. To enable alpha transparency, use PNG images with 32 bits per pixel.

    See also

    Types of Bitmaps

  • 相关阅读:
    自己写的DBHelper感慨颇深
    23种设计模式:观察者模式,第一次对观察者模式理解的这么通透。
    自己用心写的 存储过程分页 给自己的平台用
    开篇成长的开始[废话一把]
    C# 中i++在ref参数调用下的有趣现象
    点点滴滴的成长[2011111]:理解C#修饰符
    点点滴滴的成长[2011114]:自定义config文件
    扩展方法在Json字符串转化中的应用
    jquery学习二:jquery性能优化
    javascript系列1:函数
  • 原文地址:https://www.cnblogs.com/grj001/p/12223921.html
Copyright © 2011-2022 走看看