Graphics.FromImage(Image) 메서드

정의

지정된 Graphics에서 새 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

Image를 만들 Graphics입니다.

반환

이 메서드는 지정된 Graphics에 대한 새 Image를 반환합니다.

예외

image이(가) null인 경우

image에 인덱싱된 픽셀 형식이 있거나 형식이 정의되어 있지 않는 경우

예제

다음 코드 예제는 Windows Forms 사용하도록 설계되었으며 이벤트 처리기의 Paint 매개 변수인 가 필요합니다.PaintEventArgse 코드는 다음 작업을 수행합니다.

  • Image 예제 폴더의 그래픽 파일 SampImag.jpg 을 만듭니다.

  • 에서 ImageGraphics 만듭니다.

  • 이미지 내에 사각형을 채워 이미지를 변경합니다.

  • Image 를 화면에 그립니다.

  • 만든 를 해제 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

설명

이미지에 인덱싱된 픽셀 형식이 있는 경우 이 메서드는 "그래픽 개체는 인덱싱된 픽셀 형식의 이미지에서 만들 수 없습니다."라는 메시지와 함께 예외를 throw합니다. 인덱싱된 픽셀 형식은 다음 목록에 표시됩니다.

메서드를 사용하여 Save(String, ImageFormat) 인덱싱된 이미지를 다른 형식으로 저장한 다음 새 이미지에 대한 개체를 Graphics 검색할 수 있습니다.

이미지에 다음 픽셀 형식이 있는 경우에도 이 메서드는 예외를 throw합니다.

항상 메서드를 호출하여 메서드에서 DisposeGraphics 만든 FromImage 및 관련 리소스를 해제해야 합니다.

적용 대상

추가 정보