C# 如何将一个图形对象复制到另一个图形对象中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/597037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:42:24  来源:igfitidea点击:

How to copy one Graphics Object into another

c#.netvb.netgraphicsgdi+

提问by Pondidum

I am trying to copy the contents of one graphics object to another, but the only was I have been able to find is based on using GDI32.DLL, which i would rather avoid using if possible.

我试图将一个图形对象的内容复制到另一个图形对象,但我唯一能找到的是基于 using GDI32.DLL,如果可能,我宁愿避免使用。

Does anyone know how/if this is possible using managed code? I don't mind if answers are in C# or VB.Net.

有谁知道如何/是否可以使用托管代码?我不介意答案是在 C# 还是 VB.Net 中。

Here is what I currently have:

这是我目前拥有的:

Private Sub CopyGraphics()
    Dim srcPic As Graphics = pnl.CreateGraphics

    Dim srcBmp As New Bitmap(pnl.Width, pnl.Height, srcPic)
    Dim srcMem As Graphics = Graphics.FromImage(srcBmp)

    Dim HDC1 As IntPtr = srcPic.GetHdc
    Dim HDC2 As IntPtr = srcMem.GetHdc

    BitBlt(HDC2, 0, 0, pnl.Width, pnl.Height, HDC1, 0, 0, 13369376)

    pnlDraw.BackgroundImage = srcBmp

    'Clean Up code omitted...
End Sub

采纳答案by Guffa

Strictly speaking, it's not possible to copy the contents of a Graphics object anywhere using any method, because a Graphics object doesn't contain anything.

严格来说,不可能使用任何方法在任何地方复制 Graphics 对象的内容,因为 Graphics 对象不包含任何内容。

Why don't use the DrawToBitmap method to draw the control on the bitmap?

为什么不使用 DrawToBitmap 方法在位图上绘制控件?

Dim srcBmp As New Bitmap(pnl.Width, pnl.Height)
Dim clip As New Rectangle(New Point(0, 0), pnl.Size)
pnl.DrawToBitmap(srcBmp, clip)