为什么图形类的绘制图像不在我的对话框上绘制图像?


我需要在应用程序的两个对话框上各绘制一个图像。 我选择使用内存dc。 不幸的是,它没有绘制图像,而是绘制了一个黑框。 可能出什么问题了? 相关代码如下所示。

C++
//Top of dialogue box procedure

static HDC hmemDC = nullptr;
    static HBITMAP hbitmap = nullptr;
    static HBITMAP hOldbitmap = nullptr;

C++
case WM_INITDIALOG:

  HDC hdc = GetDC(hDlg);
        
        hmemDC = CreateCompatibleDC(hdc);
        hbitmap = CreateCompatibleBitmap(hmemDC,iPicWidth,iPicHeight);
        hOldbitmap = (HBITMAP)SelectObject(hmemDC, hbitmap);

        ReleaseDC(hDlg, hdc);
C++
//The drawing code

 std::string stPicPath;
            if (UpdateStudentInfoControls(hDlg, llSessionID, llArmID, eUseMode, stPicPath))
            {
               DrawAppImage(hmemDC, iPicLeft,iPicTop,iPicWidth, iPicHeight, stPicPath);
            }
            else
            {
                HPEN hPen = (HPEN)GetStockObject(BLACK_PEN);
                HPEN hOldPen = (HPEN)SelectObject(hmemDC, hPen);

                MoveToEx(hmemDC, 0, 0, NULL);
                LineTo(hmemDC,iPicWidth, 0);
                LineTo(hmemDC, iPicWidth,iPicHeight);
                LineTo(hmemDC,0,iPicHeight);
                LineTo(hmemDC,0,0);

                SelectObject(hmemDC, hOldPen);
            }
C++
case WM_PAINT:
   {
       PAINTSTRUCT ps;
       HDC hdc = BeginPaint(hDlg, &ps);

       BitBlt(hdc, iPicLeft, iPicTop, iPicWidth, iPicHeight, hmemDC, 0, 0, SRCCOPY);

       EndPaint(hDlg, &ps);

   }
   return (INT_PTR)TRUE;
C++
<


void DrawAppImage(HDC hdc, int iLeft, int iTop, int iWidth, int iHeight, string stImagePath)
{
	string stImageFullPath = stDBImageDirectory;
	stImageFullPath += '\\';
	stImageFullPath += stImagePath;

	wstring wstImagePath = utf8_decode(stImageFullPath);

	// Create an Image object.
	Gdiplus::Image image(wstImagePath.c_str());


	Gdiplus::Graphics graphics(hdc);

	// Draw the original source image.
	graphics.DrawImage(&image, iLeft, iTop, iWidth, iHeight);
}



/pre>

What I have tried:

I have spent sustantial time debugging the code.

解决方案1

1. 确保您已通过调用初始化 GDI+ 库 GdiplusStartup API。
2. 使用以下命令检查成功加载的图像 GetLastStatus 加载文件后调用 Image 类的方法。
3. 使用以下命令检查图形操作的最后状态 GetLastStatus 绘制图像后 Graphics 类的方法。
4. 确保您以正确的调用顺序执行绘图,例如您可以调用绘图并在调用后使控件的背景无效,从而导致擦除背景。 您可以首先在从 GDI+ 位图对象创建的图形对象上测试绘图,并将结果位图保存到文件中。

问候,
格言。

解决方案2

コメント

タイトルとURLをコピーしました