Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:08:32+00:00 2026-05-13T16:08:32+00:00

I have a drawing function that just takes an HDC. But I need to

  • 0

I have a drawing function that just takes an HDC.
But I need to show an EXACT scaled version of what will print.

So currently, I use
CreateCompatibleDC() with a printer HDC and
CreateCompatibleBitmap() with the printer’s HDC.

I figure this way the DC will have the printer’s exact width and height.
And when I select fonts into this HDC, the text will be scaled exactly as the printer would.

Unfortunately, I can’t to a StretchBlt() to copy this HDC’s pixels to the control’s HDC since they’re of different HDC types I guess.

If I create the “memory canvas” from a window HDC with same w,h as the printer’s page,
the fonts come out WAY teeny since they’re scaled for the screen, not page…

Should I CreateCompatibleDC() from the window’s DC and
CreateCompatibleBitmap() from the printer’s DC or something??

If somebody could explain the RIGHT way to do this.
(And still have something that looks EXACTLY as it would on printer)…

Well, I’d appreciate it !!

…Steve

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:08:33+00:00Added an answer on May 13, 2026 at 4:08 pm

    Depending on how accurate you want to be, this can get difficult.

    There are many approaches. It sounds like you’re trying to draw to a printer-sized bitmap and then shrink it down. The steps to do that are:

    1. Create a DC (or better yet, an IC–Information Context) for the printer.
    2. Query the printer DC to find out the resolution, page size, physical offsets, etc.
    3. Create a DC for the window/screen.
    4. Create a compatible DC (the memory DC).
    5. Create a compatible bitmap for the window/screen, but the size should be the pixel size of the printer page. (The problem with this approach is that this is a HUGE bitmap and it can fail.)
    6. Select the compatible bitmap into the memory DC.
    7. Draw to the memory DC, using the same coordinates you would use if drawing to the actual printer. (When you select fonts, make sure you scale them to the printer’s logical inch, not the screen’s logical inch.)
    8. StretchBlt the memory DC to the window, which will scale down the entire image. You might want to experiment with the stretch mode to see what works best for the kind of image you’re going to display.
    9. Release all the resources.

    But before you head in that direction, consider the alternatives. This approach involves allocating a HUGE off-screen bitmap. This can fail on resource-poor computers. Even if it doesn’t, you might be starving other apps.

    The metafile approach given in another answer is a good choice for many applications. I’d start with this.

    Another approach is to figure out all the sizes in some fictional high-resolution unit. For example, assume everything is in 1000ths of an inch. Then your drawing routines would scale this imaginary unit to the actual dpi used by the target device.

    The problem with this last approach (and possibly the metafile one) is that GDI fonts don’t scale perfectly linearly. The widths of individual characters are tweaked depending on the target resolution. On a high-resolution device (like a 300+ dpi laser printer), this tweaking is minimal. But on a 96-dpi screen, the tweaks can add up to a significant error over the length of a line. So text in your preview window might appear out-of-proportion (typically wider) than it does on the printed page.

    Thus the hardcore approach is to measure text in the printer context, and measure again in the screen context, and adjust for the discrepancy. For example (using made-up numbers), you might measure the width of some text in the printer context, and it comes out to 900 printer pixels. Suppose the ratio of printer pixels to screen pixels is 3:1. You’d expect the same text on the screen to be 300 screen pixels wide. But you measure in the screen context and you get a value like 325 screen pixels. When you draw to the screen, you’ll have to somehow make the text 25 pixels narrower. You can ram the characters closer together, or choose a slightly smaller font and then stretch them out.

    The hardcore approach involves more complexity. You might, for example, try to detect font substitutions made by the printer driver and match them as closely as you can with the available screen fonts.

    I’ve had good luck with a hybrid of the big-bitmap and the hardcore approaches. Instead of making a giant bitmap for the whole page, I make one large enough for a line of text. Then I draw at printer size to the offscreen bitmap and StretchBlt it down to screen size. This eliminates dealing with the size discrepancy at a slight degradation of font quality. It’s suitable for actual print preview, but you wouldn’t want to build a WYSIWYG editor like that. The one-line bitmap is small enough to make this practical.

    The good news is only text is hard. All other drawing is a simple scaling of coordinates and sizes.

    I’ve not used GDI+ much, but I think it did away with non-linear font scaling. So if you’re using GDI+, you should just have to scale your coordinates. The drawback is that I don’t think the font quality on GDI+ is as good.

    And finally, if you’re a native app on Vista or later, make sure you’ve marked your process as "DPI-aware" . Otherwise, if the user is on a high-DPI screen, Windows will lie to you and claim that the resolution is only 96 dpi and then do a fuzzy up-scaling of whatever you draw. This degrades the visual quality and can make debugging your print preview even more complicated. Since so many programs don’t adapt well to higher DPI screens, Microsoft added "high DPI scaling" by default starting in Vista.

    Edited to Add

    Another caveat: If you select an HFONT into the memory DC with the printer-sized bitmap, it’s possible that you get a different font than what would get when selecting that same HFONT into the actual printer DC. That’s because some printer drivers will substitute common fonts with in memory ones. For example, some PostScript printers will substitute an internal PostScript font for certain common TrueType fonts.

    You can first select the HFONT into the printer IC, then use GDI functions like GetTextFace, GetTextMetrics, and maybe GetOutlineTextMetrics to find out about the actual font selected. Then you can create a new LOGFONT to try to more closely match what the printer would use, turn that into an HFONT, and select that into your memory DC. This is the mark of a really good implementation.

    Another Edit

    I’ve recently written new code that uses enhanced meta files, and that works really well, at least for TrueType and OpenType fonts when there’s no font substitution. This eliminates all the work I described above trying to create a screen font that is a scaled match for the printer font. You can just run through your normal printing code and print to an enhanced meta file DC as though it’s the printer DC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem drawing something quickly in .NET. I don't think that any
I have developed application for drawing some shapes (lines mostly) , now i need
I have a view that I want to add some custom drawing to. I
I'm doing custom drawing in datagridview cells and I have items that can vertically
I wrote a drawing function that draws various on-screen sprites. These sprites can only
Currently I have an application that takes information from a SQLite database and puts
I have a project that adds elements to an AutoCad drawing. I noticed that
i have a input tag which is non editable, but some times i need
I have an object of the type System.Drawing.Image and want to make every pixel
Have just started using Google Chrome , and noticed in parts of our site,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.