图形对象未正确刷新

发布于 2024-09-11 04:16:35 字数 847 浏览 2 评论 0原文

我有一个名为“buffer”的位图,我在其中

  1. 绘制另一个图像(使用 DrawImage)
  2. 绘制部分透明的渐变(使用 LinearGradientBrush)

我在每次调用之后在缓冲区的 Graphics 对象上调用 Flush(FlushIntention.Sync)这些步骤。然后,我将缓冲区的内容绘制到屏幕控件上。

然而,在调试时,我注意到有时缓冲区没有绘制渐变。即使我显式调用同步刷新命令,什么可能导致第二个绘制操作被跳过?

有什么解决方法吗?

编辑:代码示例

Bitmap background = ....;
Bitmap buffer = new Bitmap(100, 100);
Rectangle bufferBounds = new Rectangle(0, 0, buffer.Width, buffer.Height);
Graphics bufferG = Graphics.FromImage(buffer);

// First step
bufferG.DrawImageUnscaled(background, 0, 0);
bufferG.Flush(FlushIntention.Sync);

// Second step
LinearGradientBrush lgb = new LinearGradientBrush(bufferBounds,
                maxColor, minColor, LinearGradientMode.Vertical);
bufferG.FillRectangle(lgb, bufferBounds);
bufferG.Flush(FlushIntention.Sync);

I have a Bitmap called "buffer" to which I

  1. Paint another image (using DrawImage)
  2. Paint a partially transparent gradient (using a LinearGradientBrush)

I call Flush(FlushIntention.Sync) on the buffer's Graphics object after each of those steps. I then paint the contents of the buffer onto an on-screen control.

However, while debugging, I noticed that sometimes the buffer doesn't have a gradient painted on. What can be causing the 2nd paint operation to be skipped even when I'm explicitly calling a synchronized Flush command?

Is there any workaround?

EDIT: Code sample

Bitmap background = ....;
Bitmap buffer = new Bitmap(100, 100);
Rectangle bufferBounds = new Rectangle(0, 0, buffer.Width, buffer.Height);
Graphics bufferG = Graphics.FromImage(buffer);

// First step
bufferG.DrawImageUnscaled(background, 0, 0);
bufferG.Flush(FlushIntention.Sync);

// Second step
LinearGradientBrush lgb = new LinearGradientBrush(bufferBounds,
                maxColor, minColor, LinearGradientMode.Vertical);
bufferG.FillRectangle(lgb, bufferBounds);
bufferG.Flush(FlushIntention.Sync);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2024-09-18 04:16:35

我注意到您没有在 using 块中创建 Graphics 对象。是否需要 Dispose() 来完全刷新它?

using (Graphics bufferG = Graphics.FromImage(buffer)
{
...
}

I noticed you don't create the Graphics object in a using block. Could Dispose() be needed to fully flush it?

using (Graphics bufferG = Graphics.FromImage(buffer)
{
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文