방법: GDI+를 사용하여 이미지 렌더링

애플리케이션에서 파일로 존재하는 이미지를 렌더링하는 데 GDI+를 사용할 수 있습니다. 이 작업을 수행하려면 Image 클래스의 새 개체(예: Bitmap)를 만들고 사용할 그리기 화면을 참조하는 Graphics 개체를 생성한 다음, Graphics 개체의 DrawImage 메서드를 호출합니다. 이미지는 그래픽 클래스에서 표시하는 그리기 화면에 그려집니다. 이미지 편집기를 사용하여 디자인 타임에 이미지 파일을 만들고, 편집하고, 런타임 시 GDI+를 사용하여 렌더링합니다. 자세한 내용은 아이콘에 대한 이미지 편집기를 참조하세요.

GDI+를 사용하여 이미지를 렌더링하려면

  1. 표시하려는 이미지를 나타내는 개체를 만듭니다. 이 개체는 Bitmap 또는 Metafile과 같이 Image에서 상속되는 클래스의 멤버여야 합니다. 예제를 다음과 같이 보여 줍니다.

    ' Uses the System.Environment.GetFolderPath to get the path to the
    ' current user's MyPictures folder.  
    Dim myBitmap as New Bitmap _  
       (System.Environment.GetFolderPath _  
          (System.Environment.SpecialFolder.MyPictures))  
    
    // Uses the System.Environment.GetFolderPath to get the path to the
    // current user's MyPictures folder.  
    Bitmap myBitmap = new Bitmap  
       (System.Environment.GetFolderPath  
          (System.Environment.SpecialFolder.MyPictures));  
    
    // Uses the System.Environment.GetFolderPath to get the path to the
    // current user's MyPictures folder.  
    Bitmap^ myBitmap = gcnew Bitmap  
       (System::Environment::GetFolderPath  
          (System::Environment::SpecialFolder::MyPictures));  
    
  2. 사용하려는 그리기 화면을 나타내는 Graphics 개체를 만듭니다. 자세한 내용은 방법: 그리는 데 필요한 그래픽 개체 만들기를 참조하세요.

    ' Creates a Graphics object that represents the drawing surface of
    ' Button1.  
    Dim g as Graphics = Button1.CreateGraphics  
    
    // Creates a Graphics object that represents the drawing surface of
    // Button1.  
    Graphics g = Button1.CreateGraphics();  
    
    // Creates a Graphics object that represents the drawing surface of
    // Button1.  
    Graphics^ g = button1->CreateGraphics();  
    
  3. 그래픽 개체의 DrawImage를 호출하여 이미지를 렌더링합니다. 가져올 이미지 및 이미지를 가져올 좌표를 지정해야 합니다.

    g.DrawImage(myBitmap, 1, 1)  
    
    g.DrawImage(myBitmap, 1, 1);  
    
    g->DrawImage(myBitmap, 1, 1);  
    

참고 항목