Graphics.FromImage(Image) 方法

定义

从指定的 Graphics 创建新的 ImageCreates a new Graphics from the specified Image.

public:
 static System::Drawing::Graphics ^ FromImage(System::Drawing::Image ^ image);
public static System.Drawing.Graphics FromImage (System.Drawing.Image image);
static member FromImage : System.Drawing.Image -> System.Drawing.Graphics
Public Shared Function FromImage (image As Image) As Graphics

参数

image
Image

从中创建新 GraphicsImageImage from which to create the new Graphics.

返回

Graphics

此方法为指定的 Graphics 返回一个新的 ImageThis method returns a new Graphics for the specified Image.

例外

imagenullimage is null.

image 具有索引像素格式,或者格式未定义。image has an indexed pixel format or its format is undefined.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要作为 PaintEventArgs e Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following action:

  • Image从示例文件夹中 SampImag.jpg 图形文件创建。Creates an Image from a graphics file SampImag.jpg in the example folder.

  • 从创建一个 Graphics ImageCreates a Graphics from the Image.

  • 通过填充图像中的矩形来更改图像。Alters the image by filling a rectangle within it.

  • 将绘制 Image 到屏幕。Draws the Image to the screen.

  • 释放创建的 GraphicsReleases the created Graphics.

public:
   void FromImageImage( PaintEventArgs^ e )
   {
      // Create image.
      Image^ imageFile = Image::FromFile( "SampImag.jpg" );

      // Create graphics object for alteration.
      Graphics^ newGraphics = Graphics::FromImage( imageFile );

      // Alter image.
      newGraphics->FillRectangle( gcnew SolidBrush( Color::Black ), 100, 50, 100, 100 );

      // Draw image to screen.
      e->Graphics->DrawImage( imageFile, PointF(0.0F,0.0F) );

      // Dispose of graphics object.
      delete newGraphics;
   }
private void FromImageImage(PaintEventArgs e)
{

    // Create image.
    Image imageFile = Image.FromFile("SampImag.jpg");

    // Create graphics object for alteration.
    Graphics newGraphics = Graphics.FromImage(imageFile);

    // Alter image.
    newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);

    // Draw image to screen.
    e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));

    // Dispose of graphics object.
    newGraphics.Dispose();
}
Private Sub FromImageImage2(ByVal e As PaintEventArgs)

    ' Create image.
    Dim imageFile As Image = Image.FromFile("SampImag.jpg")

    ' Create graphics object for alteration.
    Dim newGraphics As Graphics = Graphics.FromImage(imageFile)

    ' Alter image.
    newGraphics.FillRectangle(New SolidBrush(Color.Black), _
    100, 50, 100, 100)

    ' Draw image to screen.
    e.Graphics.DrawImage(imageFile, New PointF(0.0F, 0.0F))

    ' Dispose of graphics object.
    newGraphics.Dispose()
End Sub

注解

如果图像具有索引像素格式,则此方法将引发异常,并出现消息 "无法从具有索引像素格式的图像创建图形对象。"If the image has an indexed pixel format, this method throws an exception with the message, "A Graphics object cannot be created from an image that has an indexed pixel format." 下面的列表显示了索引像素格式。The indexed pixel formats are shown in the following list.

您可以通过使用方法将索引图像另存为另一种格式 Save(String, ImageFormat) ,然后检索 Graphics 新图像的对象。You can save the indexed image as another format by using the Save(String, ImageFormat) method and then retrieve a Graphics object for the new image.

如果图像具有以下任意像素格式,则此方法也会引发异常。This method also throws an exception if the image has any of the following pixel formats.

应始终调用 Dispose 方法以释放 Graphics 由方法创建的和相关资源 FromImageYou should always call the Dispose method to release the Graphics and related resources created by the FromImage method.

适用于