Print a Form at higher dpi than screen resolution

眉间皱痕 提交于 2019-12-01 01:27:29

Okay, I have figured this out... and it works great!

I still don’t know how to create a form at 300dpi and use the auto-scaling functionality.

BUT…

I have proven that if you create the window 3.125x larger than needed at 96 dpi, and scale the font up 3.125x, and so on, such that everything is the pixel count you’d need to be at 300dpi, even though your screen is at 96dpi, then you can use the normal Control.DrawToBitmap() functionality to turn that into a Bitmap, and then you can use the GDI Graphics.DrawImage(thatGiantBitmap, giantSrcRect, pageSizeDestRect) to the printer Graphics object, and it will map those giant 96dpi pixels to the page-size 300dpi pixels, giving you a 300dpi print. Perfect.

For any of our windows that support resizing AND let our users zoom the contents arbitrarily, then printing What-You-See-Is-What-You-Get is easy:

In OnBeginPrint of your PrintDocument, do:

(1) Optionally dup the Form so as to not mess with what the user is looking at

(2) Move the Form you want to print off-screen (below and right of all your screens)

(3) Set the Form so that it is allowed to grow bigger than the screen size (by default WinForms won’t grow larger than the screen)

(4) Divide 300 dpi by your screen dpi to get the growth factor

(5) Grow your Form by growth factor

(6) Zoom its contents by growth factor (if they don’t auto-scale / auto-zoom with the Form’s size)

In OnPrintPage of your PrintDocument, do:

(7) On whatever Control in your Form you want to print, do DrawToBitmap() to a Bitmap the size of that Control

(8) On the e.Graphics do DrawImage(thatGiantBitmap, giantSrcRect, pageSizeDestRect)

That DrawImage call will draw at the printer’s resolution, if you have that many pixels in your thatGiantBitmap. In this case, I computed the Bitmap to give the number of pixels needed for 300 dpi, so I will get 300 dpi print-outs even if the printer is 600 dpi. If you need full 600 dpi, just use 600 dpi in step 4's calculation.

Total guess here, but what about using CSS? I'm guessing at how much you want to scale, and don't know how you would know what scale the printer is. Using the print media query makes this work for print but leaves the screen view alone.

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