实际上,Yorye Nathan 在 2012 年 6 月 3 日 7:33 的回答是正确的,这是帮助我的起点。但是,我无法让它按原样工作,所以我做了一些更正以使其在我的应用程序中工作。更正方法是从 PrintPgeEventArgs.Graphics 设备上下文中获取打印机的页面大小,并将 PrintPage Graphics 作为第三个参数包含在新的 Bitmap(...) 构造中。
private void doc_PrintPage(object sender, PrintPageEventArgs ppea)
{
// Retrieve the physical bitmap boundaries from the PrintPage Graphics Device Context
IntPtr hdc = ppea.Graphics.GetHdc();
Int32 PhysicalWidth = GetDeviceCaps(hdc, (Int32)PHYSICALWIDTH);
Int32 PhysicalHeight = GetDeviceCaps(hdc, (Int32)PHYSICALHEIGHT);
ppea.Graphics.ReleaseHdc(hdc);
// Create a bitmap with PrintPage Graphic's size and resolution
Bitmap myBitmap = new Bitmap(PhysicalWidth, PhysicalHeight, ppea.Graphics);
// Get the new work Graphics to use to draw the bitmap
Graphics myGraphics = Graphics.FromImage(myBitmap);
// Draw everything on myGraphics to build the bitmap
// Transfer the bitmap to the PrintPage Graphics
ppea.Graphics.DrawImage(myBitmap, 0, 0);
// Cleanup
myBitmap.Dispose();
}
////////
// Win32 API GetDeviceCaps() function needed to get the DC Physical Width and Height
const int PHYSICALWIDTH = 110; // Physical Width in device units
const int PHYSICALHEIGHT = 111; // Physical Height in device units
// This function returns the device capability value specified
// by the requested index value.
[DllImport("GDI32.DLL", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 nIndex);
再次感谢 Yorye Nathan 提供原始答案。//AJ