图形对象未正确刷新
我有一个名为“buffer”的位图,我在其中
- 绘制另一个图像(使用 DrawImage)
- 绘制部分透明的渐变(使用 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
- Paint another image (using DrawImage)
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到您没有在 using 块中创建 Graphics 对象。是否需要 Dispose() 来完全刷新它?
I noticed you don't create the Graphics object in a using block. Could Dispose() be needed to fully flush it?