0

我已经看到,绘制两个图像非常耗时,所以我想尽可能减少绘制这些图像。我使用图片编辑并覆盖了 ondraw 事件。在这里,我添加了以下部分,但它最终变成了黑色图像。我确实想检查一下,我是否需要画画。此检查有效。然后我绘制背景并将其覆盖在另一个背景上,我想将其存储为图像并将其作为我的控件的背景。

如果我不重绘背景,我只需调用:

g.Clear(Color.Transparent);

在我的图片编辑绘画事件中。

我有什么问题吗?

此致,

帕特里克

//Recalculate the background
if (redrawBackground)
{
    g.FillRectangle(Brushes.White, 0, 0, backGroundImage.Width, backGroundImage.Height);
    g.Clear(Color.White);
    //this is drawn without aspect ration. The matrix is stored, since drawing images is expensive
    GenerateTransformation(g, false);

    g.DrawImageUnscaled(backGroundImage, new Rectangle(0, 0, backGroundImage.Width, backGroundImage.Height));
    lastTransformation = g.Transform;

    GenerateTransformation(g, true);

    g.DrawImage(foreGroundImage, new Rectangle(0, 0, backGroundImage.Width, backGroundImage.Height));
    lastTransformationAspect = g.Transform;
    //Save bitmap as background
    Bitmap bmp = new Bitmap(backGroundImage.Width, backGroundImage.Height, e.Graphics);
    //Trick the background by a new image
    pictureEdit.Image = bmp;
}
4

1 回答 1

1

您的代码 ( new Bitmap(backGroundImage.Width, backGroundImage.Height, e.Graphics); ) 不会将图像复制到位图;它仅将 Graphics 对象与位图相关联。

尝试这个:

Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.DrawSomething(); // This draws to the bitmap
于 2013-02-21T15:59:29.390 回答