如何在 C# 中克隆图形?

发布于 2024-10-06 08:13:12 字数 336 浏览 0 评论 0原文

我想为应用程序的不同部分提供独立的 Graphics 实例,这些实例最终在相同的基础 Graphics 上进行绘制。简单地克隆 Graphics 是可行的,但由于两个实例都引用相同的 GDI 句柄,因此它们并不独立。我也无法使用 Begin 和 EndContainer,因为我有一个必须提供新 Graphics 实例的方法。 - 所以我无法确定何时调用 EndContainer。该用例与 Java 中的 Graphics.create() 方法非常相似。

我找到了一些解决方法,但它们都不适用于 PrintController 提供的 Graphics。

有我可以使用的代理图形吗?或者是否有可能为同一设备创建另一个图形?

I want to provide different parts of an application with independent Graphics instances which end up painting on the same base Graphics. Simply cloning the Graphics works, but since both instances refer to the same GDI handle, there are not independent. I can't use Begin and EndContainer as well since I have a method which has to provide the new Graphics instances. -so I cannot determine when to call EndContainer. The use case is quite similar to the Graphics.create() method in Java.

I've found some workarounds, but none of them works for a Graphics provided by the PrintController.

Is there any proxy-Graphics I can use? Or is there a possibility to create another Graphics for the same device for instance?

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

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

发布评论

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

评论(5

走过海棠暮 2024-10-13 08:13:12

这听起来很糟糕。不要存储对 Graphics 对象的引用,它仅暂时存在,并且仅在 Paint 或 PrintPage 事件处理程序运行时有效。请确保将其作为参数传递给进行绘图的任何方法,而不是将其存储在字段或全局变量中。

如果该方法正在更改对象的状态,则使用 Save() 和 Restore() 方法来防止这在使用同一对象的后续方法中引起问题。使用这种方法永远不需要克隆它。

This sounds bad. Do not store references to a Graphics object, it only ever lives temporarily and is only valid while a Paint or PrintPage event handler is running. Do make sure to pass it as an argument to whatever method does the drawing instead of storing it in a field or a global variable.

If the method is altering the state of the object then use the Save() and Restore() methods to prevent this from causing problems in subsequent methods that use that same object. Cloning it is never necessary with this approach.

极度宠爱 2024-10-13 08:13:12

图形对象并不意味着被持久化。您可以在最终渲染之前通过绘制位图来使用后备缓冲区方法。

也许您可以引发一个监听绘图组件可以订阅的事件,并且您的调用代码可以将这些事件链接在一起。这样您就可以使用相同的 Graphics 实例,而不会影响 GDI 效率。

Graphics objects are not meant to be persisted. You could use a backbuffer approach by drawing to a Bitmap before your final render.

Perhaps you could raise an event to which listening drawing components could subscribe, and your calling code could chain these together. That way you could use the same Graphics instance without compromising GDI efficiency.

擦肩而过的背影 2024-10-13 08:13:12

不确定您到底想做什么,但您可以在 ControlGraphics.FromImage(xx) 上使用 CreateGraphics() 来创建控件和/或图像的新 Graphics 对象。 Graphics.FromXXX 中还有更多函数

Not sure what exactly you're trying to do but you can use CreateGraphics() on a Control or Graphics.FromImage(xx) to create a new Graphics object for the control and/or image. There's also a few more functions in Graphics.FromXXX

酸甜透明夹心 2024-10-13 08:13:12

一种可能性是创建指向多个目标的多个图形对象,例如内存图像。完成后,将所有图像合并为一张。

但我不明白的是,如果所有图形实例都应该绘制到同一个目标,为什么首先需要多个图形对象?

A possibility would be to create multiple graphics objects which are pointing to multiple targets, for example an memory image. Then after done, combine all images into one.

But the thing I don't understand is, if all graphics instances should paint to the same target why do you need multiple graphics objects in the first place?

夏了南城 2024-10-13 08:13:12

我面临着同样的问题,我发现唯一的解决方案是复制图纸代码行!

就像下面这样:

 e.Graphics.DrawString(points(i).pointText, myFont, Brushes.Blue, New Point(points(i).crossPointX4, points(i).crossPointY4)) : G.DrawString(points(i).pointText, myFont, Brushes.Blue, New Point(points(i).crossPointX4, points(i).crossPointY4))

I was facing same problem, I found the only solution is to duplicate the drawings code line !!

Like the following:

 e.Graphics.DrawString(points(i).pointText, myFont, Brushes.Blue, New Point(points(i).crossPointX4, points(i).crossPointY4)) : G.DrawString(points(i).pointText, myFont, Brushes.Blue, New Point(points(i).crossPointX4, points(i).crossPointY4))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文