【发布时间】:2009-10-18 05:41:57
【问题描述】:
我正在使用 C++ 编写一个 Win32 应用程序。
在这个应用程序中,我正在处理 WM_PAINT 消息:
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GdiplusStartup(&gdiplusToken, &gdiPlusStartup, 0);
DrawM(ps.hdc, hWnd);
EndPaint(hWnd, &ps);
break;
在 DrawM 函数中我有这样的东西:
void DrawMap(HDC hdc, HWND hWnd)
{
if(!isDrawn)
{
// (some calculations)
Graphics g(hdc);
Bitmap img(max_x, max_y, &g);
int zoom_factor = 50;
for(int i = 0; i< segments.size(); i++)
{
// (some math)
for(int j = 0; j < segments.at(i).point_count; j++)
// (another dose of math)
g.DrawLines(&pen, segmentPoints, segments.at(i).point_count);
delete [] segmentPoints;
}
g.Save();
isDrawn = true;
}
else
{
// here is the problem
}
}
在上面的代码中,我想做的是渲染一次图像,然后当窗口调整大小、移动或发生任何需要重新绘制的事情时,不会从头开始渲染位图,而是应该使用缓存的。
问题是 Bitmap 不允许复制(复制构造函数拒绝它)。 另一个问题是,当我尝试将图像保存到文件或流时,我收到“无效参数”错误(即返回码为 2):
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
img.Save(_T("m.png"), &Gdiplus::ImageFormatPNG, NULL);
->clone() 似乎也不起作用,因为当我定义指向位图的指针时,将位图克隆到它并在我使用的“else”语句中:
Graphics g(hdc);
g.DrawImage(bmpClone, 50, 50);
没有渲染任何东西。
关于如何缓存位图的任何想法?
【问题讨论】: