2014-04-10 3 views
0

WPF에서 새로 생겼습니다.wpf의 윈도우에 비트 맵을 그립니다.

비트 맵을로드하고 창에 일부분을 그려주는 mfc 코드를 구현했습니다. 이제 WPF에서 비슷한 기능을 이식하려고합니다.

내 WPF 창에 비트 맵을 그리고 일부 작업을 수행하려고합니다. 창에는 최소, 최대, 닫기 버튼이 없습니다.

아래는 내 mcc 코드입니다.

LRESULT Mywindow::OnPaint(UINT, WPARAM, LPARAM, BOOL&) 
     { 
      CPaintDC dc(this); 
     CDC cdc; 
     HDC hdc = dc.GetSafeHdc(); 
     cdc.Attach(hdc); 
     paintBitmapOnWindow(&cdc); 
     } 

    void Mywindow::paintBitmapOnWindow(CDC* pCdc) 
    { 
     HBITMAP backgroundBitmap; 
     backgroundBitmap = (HBITMAP)::LoadImage(0, _T("C:\\temp.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 
     CDC cdcCompatible; 
     cdcCompatible.CreateCompatibleDC(pCdc); 
     cdcCompatible.SelectObject(backgroundBitmap); 

     CRect clientRect; 
     GetClientRect(clientRect); 

     int clientHalfWidth = 800/2; 
     CRect clientLeft; 
     CRect imageLeft; 
     BITMAP bm; 
     ::GetObject(backgroundBitmap, sizeof(BITMAP), (PSTR)&bm); 

     clientLeft.SetRect(clientRect.left, clientRect.top, clientRect.left + clientHalfWidth, clientRect.bottom); 

     int bitmapHalfWidth = bm.bmWidth/2; 
     int bitmapLeftOffset = bitmapHalfWidth - 0; 

     imageLeft.SetRect(bitmapLeftOffset - clientLeft.Width(), 0, bitmapLeftOffset, bm.bmHeight); 

     pCdc->StretchBlt(clientLeft.left, clientLeft.top, clientLeft.Width(), clientLeft.Height(), &cdcCompatible, 
      imageLeft.left, imageLeft.top, imageLeft.Width(), imageLeft.Height(), SRCCOPY); 
     cdcCompatible.DeleteDC(); 
    } 

WPF로 변환하는 데 사용할 수있는 비슷한 기능/클래스는 무엇입니까?

+1

[벡터 그래픽] (http://msdn.microsoft.com/ko-kr/default.aspx)에 포함 된 WPF를 사용하는 것이 좋습니다. us/library/ms747393 (v = vs.110) .aspx) 기능을 사용합니다. 비트 맵은 확장 성이 좋지 않으며 큰 화면 해상도에서는 매우 픽셀 화 된 UI로 끝나고 벡터 그래픽은 품질을 잃지 않고 끝없이 확장됩니다. –

답변

1

비트 맵을 창에 직접 blit하려는 경우 렌더링을 직접 제어하려는 경우 OnRender 기능을 무시할 수 있습니다. 그러나 catch (코드 주석에 명시된)에 있습니다 ...

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.Background = Brushes.Transparent; // <-- necessary (strange bug) 
     // Charles Petzold writes about this at the following link: 
     // http://social.msdn.microsoft.com/Forums/vstudio/en-US/750e91c2-c370-4f0a-b18e-892afd99bd9b/drawing-in-onrender-beginnerquestion?forum=wpf 
    } 

    protected override void OnRender(DrawingContext drawingContext) 
    { 
     base.OnRender(drawingContext); 
     BitmapSource bitmapSource = new BitmapImage(new Uri("C:\\Temp.png", UriKind.Absolute)); 
     CroppedBitmap croppedBitmap = new CroppedBitmap(bitmapSource, new Int32Rect(20, 20, 100, 100)); 
     drawingContext.DrawImage(croppedBitmap, new Rect(0.0d, 0.0d, this.RenderSize.Width/2.0d, this.RenderSize.Height)); 
    } 
} 
+0

고마워, 그 작동하지만 우리는뿐만 아니라 이미지 사각형을 지정할 수있는 MFC의 StretchBlt() 메서드와 비슷한 전체 이미지 대신 이미지의 일부를 그려 싶습니다. – user3106005

+0

@ user3106005 - 이미지의 일부만 렌더링하도록 코드를 업데이트했습니다. 희망이 도움이됩니다. – Jeff

관련 문제