Graphics.FromImage(Image) 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
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.GraphicsPublic Shared Function FromImage (image As Image) As Graphics参数
返回
例外
              image
              null。
              image 具有索引像素格式或其格式未定义。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
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
注解
如果图像具有索引像素格式,此方法将引发消息异常:“无法从具有索引像素格式的图像创建图形对象。索引像素格式显示在以下列表中。
可以使用 Save(String, ImageFormat) 方法将索引图像保存为另一种格式,然后检索新图像的 Graphics 对象。
如果图像具有以下任何像素格式,此方法也会引发异常。
应始终调用 Dispose 方法来释放 FromImage 方法创建的 Graphics 和相关资源。
适用于
另请参阅
- 如何:在运行时创建位图
- 处理图像、位图、图标和图元文件
- 如何:为绘图 创建图形对象