Solved

Printing with MFC sometime result in missing parts

Posted on 2010-09-17
12
419 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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