Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
缓存位图 我用C++编写Win32应用程序。_C++_Winapi_Gdi+ - Fatal编程技术网

缓存位图 我用C++编写Win32应用程序。

缓存位图 我用C++编写Win32应用程序。,c++,winapi,gdi+,C++,Winapi,Gdi+,在此应用程序中,我处理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 h

在此应用程序中,我处理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
} 
->clone()似乎也不起作用,因为当我定义指向位图的指针时,将位图克隆到它,并在我使用的“else”语句中:

    Graphics g(hdc);
    g.DrawImage(bmpClone, 50, 50);
没有渲染任何内容


有没有关于如何缓存位图的想法?

Clone()
应该可以工作,但是如果没有看到使用位图的代码,就很难知道发生了什么。作为替代方案,另一种(更迂回的)方法是在原始
位图上调用
GetHBITMAP()
,存储GDI位图句柄,然后在以后的重新绘制中使用构造函数构造新的
位图

不要将img声明为本地对象,而是将其作为静态对象或类的成员。然后它将在下一次WM_绘制时可用,无需复制。

我不能。因为,要构建img,我需要知道高度和宽度,这是基于我在Draw方法中所做的计算来计算的。我不能创建一个简单的位图:img;然后将其分配给:img=Bitmap(…),因为它没有无参数构造函数。如果这可以解决,我的问题就会解决。那么为什么不保存一个指向位图的指针呢?这样你就不需要预先分配了。
    Graphics g(hdc);
    g.DrawImage(bmpClone, 50, 50);