C# 图形对象的翻译不';不影响WM_打印调用

C# 图形对象的翻译不';不影响WM_打印调用,c#,graphics,gdi+,sendmessage,C#,Graphics,Gdi+,Sendmessage,我目前正在使用WM_PRINT调用将控件渲染为图形对象: GraphicsState backup = graphics.Save(); graphics.TranslateTransform(50, 50); IntPtr destHdc = graphics.GetHdc(); const int flags = (int)(DrawingOptions.PRF_CHILDREN | DrawingOptions.PRF_CLIENT | DrawingOptions.PRF_NONCL

我目前正在使用WM_PRINT调用将控件渲染为图形对象:

GraphicsState backup = graphics.Save();
graphics.TranslateTransform(50, 50);

IntPtr destHdc = graphics.GetHdc();

const int flags = (int)(DrawingOptions.PRF_CHILDREN | DrawingOptions.PRF_CLIENT | DrawingOptions.PRF_NONCLIENT);
NativeMethods.SendMessage(srcControl.Handle, (Int32)WM.WM_PRINT, (IntPtr)destHdc, (IntPtr)flags);
graphics.ReleaseHdc(destHdc);
graphics.DrawLine(Pens.Blue, new Point(), new Point(srcControl.Width, srcControl.Height));

graphics.Restore(backup);
我需要使用WM_PRINT命令而不是control.DrawToBitmap(),因为DrawToBitmap方法不处理屏幕外的控件

代码将正确地将蓝线的图形变换50,50,但控件渲染在左上角(0,0)。是否有任何方法可以使用WM_PRINT命令打印到特定位置(50,50)


谢谢

原因是
WM_PRINT
使用
设备上下文
,而不是通过
图形
,因此
转换
不受影响。它仅受
图形
上调用的绘图方法的影响。 以下是一个解决方法:

GraphicsState backup = graphics.Save();
Bitmap bm = new Bitmap(srcControl.Width, srcControl.Height);
Graphics g = Graphics.FromImage(bm);
IntPtr destHdc = g.GetHdc();

const int flags = (int)(DrawingOptions.PRF_CHILDREN | DrawingOptions.PRF_CLIENT |     DrawingOptions.PRF_NONCLIENT);
NativeMethods.SendMessage(srcControl.Handle, (Int32)WM.WM_PRINT, destHdc,  (IntPtr)flags);
g.ReleaseHdc(destHdc);
graphics.DrawImage(bm, new Point(50,50));
graphics.DrawLine(Pens.Blue, new Point(), new Point(srcControl.Width, srcControl.Height));

graphics.Restore(backup);