如何缓存控件的图像?

时间:2014-11-18 12:19:40

标签: c# winforms invalidation

我有C#app,但绘画工作是在C ++项目中完成的(这就是 )。

所以我添加到了我的窗口PictureBox,并将句柄传递给绘图函数。它工作正常。

然而,当控件无效时(我将窗口移到屏幕外然后将其移回),我得到结果部分为空的窗口。 AFAIK有两种方法 - 重绘或缓存图像。

重绘可能是一个代价高昂的过程(图像不会经常变化,但处理很费时,想想3d渲染器),所以我想简单地缓存图像。

如果我从使用Win32 API直接编程的过去中记得正确,只需设置一些标志即可。 WinForms可以吗?如果是,怎么样?

1 个答案:

答案 0 :(得分:1)

这会有所帮助:

Bitmap getControlSurface(Control ctl)
{
    bitmap = new Bitmap(ctl.Width, ctl.Height);

    using (Graphics g = Graphics.FromImage(bitmap))
    {
        Point pScreen = PointToScreen(ctl.Location);
        g.CopyFromScreen(pScreen, Point.Empty, ctl.Size);
    }
    return bitmap;
}

如果您知道表面何时刷新,可以像这样使用它:

 if ( pictureBox1.Image != null) pictureBox1.Image.Dispose();
 pictureBox1.Image =  getControlSurface(pictureBox1);

唯一的问题是PictureBox实际上必须在屏幕上完全可见

相关问题