How to copy one Graphics Object into another

微笑、不失礼 提交于 2019-12-18 06:56:33

问题


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.

Does anyone know how/if this is possible using managed code? I don't mind if answers are in C# or 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

回答1:


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.

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

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


来源:https://stackoverflow.com/questions/597037/how-to-copy-one-graphics-object-into-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!