How to copy one graphic to another

I am trying to copy the contents of one graphic to another, but the only thing I could find is based on using GDI32.DLL , which I would prefer to avoid if possible.

Does anyone know how this is possible using managed code? I do not mind if the answers are in C # or VB.Net.

Here is what I have now:

 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 
+4
source share
1 answer

Strictly speaking, it is not possible to copy the contents of a Graphics object anywhere using any method because the Graphics object does not contain anything.

Why not use the DrawToBitmap method to draw a control in a 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) 
+5
source

All Articles