Solved

Printing with MFC sometime result in missing parts

Posted on 2010-09-17
12
422 Views
Last Modified: 2013-11-20
I am writing a program in MFC that prints very large drawings. I am using standard printing methods, and uses the same drawing code for screen and for printing. Most of the time this works fine, but with very large drawings parts og my print is missing. The error never occurs on the screen. Missing parts appears to be random, and I cannot reproduce the problem. It happens also when using other printers, like printing to pdf. What could I be doing wrong?
0
Comment
Question by:Ruskialt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33705278
You need to show the code.
You need to explain more. "standard printing methods" says nothing. I can guess that you use GDI (or an MFC wrapper) to draw an image. If it is so, probably, there is a GDI leak in the drawing procedure. For example, a bitmap forgotten in the device context.
0
 
LVL 2

Author Comment

by:Ruskialt
ID: 33705406
I have created a Doc/View project in VS6, and do all my drawing is in OnDraw(CDC* pDC). I use the standard printing functionality built into the template that creates the doc/view project. So I don't know what code to show, as I cannot point to anything special.

Is it possible there is a limit to how much data can be sent to the printer?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33705479
>>Is it possible there is a limit to how much data can be sent to the printer?
I don't think so. How big is the image you are drawing? If it's too big for the printing, it is for the screen view drawing too. Do you create an internal bitmap? How big is it?

Usually the problem is in OnDraw(CDC* pDC) method. Check that you don't have a GDI leak there. If you say that this is a random bug, it's most likely a leak.
Beside OnDraw you use other methods: OnPreparePrinting, OnBeginPrinting, OnPrepareDC?
There is a leak/s in one of these methods.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33705494
Here is an article about the MFC printing (VS6):
http://www.dotnetheaven.com/Uploadfile/mahesh/MFCPrinting05242005051835AM/MFCPrinting.aspx
There is a code attached too.
0
 
LVL 2

Author Comment

by:Ruskialt
ID: 33705504
How can I check for leaks, any typical GDI leaks/pitfalls?
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33706915
If you have Bound Checker, it will show everything.
You can simply check GDI objects in Task Manager, so you will see the counter for your process.
There is a special tool GDIView (?): http://www.nirsoft.net/utils/gdi_handles.html

You can verify your code:
1. If you cal GetDC, you need to call ReleaseDC when you already don't need this DC.
2. If you call CreateDC, CreateCompatibleDC, you call DeleteDC in the end.
3. If you create a bitmap or a font, delete them.
4. If you select a bitmap,a font in a DC, you save the old GDI object and restore it in the end.

The last item is very important. Almost always the leak is there. The DC object is not deleted if it contains a bitmap or a font. So if you have:
HDC hDC = GetDC(hWnd);
HDC hMemDC = CreateCompatibleBitmap(hDC);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);

do not forget:
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
ReleaseDC(hWnd, hDC);
0
 
LVL 2

Author Comment

by:Ruskialt
ID: 33732313
I got notified on inactive question. I am still working with your suggestions. I found no leaks in task managers gdi object count, does this mean that 1, 2 and 3 is no problem? (Still searching my code for violations to all of this) What is Bound checker?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33734695
It's a commercial product. Use Google. The price is about 1000$.

But... it's really inactive question. You say that you used a task manager and didn't find any leak with the GDI counter. Please do not think that it's sarcasm, but it's not serious. If you really tried that, you saw that this counter changes all the time. Probably its value is about 100-200. Let's say before this problematic print operation the value was 154. When the printing finished it's 154 again. If it is so, your application is perfect and you can be proud and show the printing code here.  
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33734769
You can use GDIView: http://www.nirsoft.net/utils/gdi_handles.html
It's not Bound Checker, but it will detect the leaks.
0
 
LVL 2

Author Closing Comment

by:Ruskialt
ID: 33763857
Brilliant thinking
0
 
LVL 2

Author Comment

by:Ruskialt
ID: 33763867
Thanks for all your help. I am still digging through my codes looking for any of your suggestions. It is a large project, and I have other tasks at work to, so progress on this is a little slow. I have found some leaks, but have yet to conclude that this fixes the printing problem. However I want to close this and dedicate my points to excellent answers and suggestions.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33765099
Thank you too.
Good luck.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replacement selected text 2 67
Looking for a specific application/software 2 119
fizzArray  challenge 1 109
How to convert MFC::CString to UTF8 wchar_t* 10 384
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question