Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

c# - How to copy one Graphics Object into another

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...