Graphics.FromImage(Image) 方法


從指定的 Graphics 建立新 ImageCreates a new Graphics from the specified Image.

 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



用來建立新 GraphicsImageImage from which to create the new 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 Forms 搭配使用所設計,而且需要 PaintEventArgse,這是 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:

  • 從範例資料夾中的 SampImag 圖形檔案建立 ImageCreates an Image from a graphics file SampImag.jpg in the example folder.

  • Image建立 GraphicsCreates 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.

   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.
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.
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 方法,以釋放 FromImage 方法所建立的 Graphics 和相關資源。You should always call the Dispose method to release the Graphics and related resources created by the FromImage method.