## TPrintPreview.PaintGraphicEx shows BMP as black blocks

Please post bug reports, feature requests, or any question regarding the DELPHI AREA projects here.

### TPrintPreview.PaintGraphicEx shows BMP as black blocks

In recent changes to my sofware I am randomly getting black blocks instead of BMP graphics when using PaintGraphicEx on the TPrintPreview component.

I have been using TPrintPreview in the development of Delphi7-compiled business management software for a company for more than 10 years. (Best postcard I ever sent - thanks, Kambiz!) To-date I was using Preview v4.75 but after the problem occurred, I have tried v5.70 and v5.90 unsuccessfully. Suprisingly, attempts to re-create the previous revision of my software using saved source code did not get rid of the problem.

BMPs (e.g. the company logo), saved as resources in my .exe file, are copied to the TPrintPreview component using PrintGraphicEx. Most of the time this works. But after some program usage, it seems something goes wrong and images predominantly appear as black blocks. (I have checked that I am Free-ing all dynamically created objects, so as not to create memory leaks).

Errors have been seen on Windows XP, 7 and 8.1. Development/compilation has been tried under both Windows XP and Windows 7 - I'm considering porting the whole development to Windows 10 and Delphi XE10 Seattle in a hopeful attempt to kill the problem)

Any suggestions would be much appreciated.

Attached is a screenshot (with some privacy blurring) showing 4 pages of an invoice with some BMPs reproduced correctly and others showing black.

Thanks for the help!

Andrew
Attachments
BlackBlocks.jpg (180.97 KiB) Viewed 2401 times
AndrewTBDev
Member

Posts: 3
Joined: September 3rd, 2013, 10:09 am

### Re: TPrintPreview.PaintGraphicEx shows BMP as black blocks

Hi Andrew,

• Do you destroy the bitmap after passing it to PaintGraphicEx?
• What if you load the bitmap from a file instead of the resource?
• Is the bitmap a device independent bitmap? Try to set PixelFormat property of the bitmap to either pf24bit or pf32bit.
• Is the bitmap's Transparent property false?
And, please give me one of your bitmaps, so that I can regenerate the problem.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: TPrintPreview.PaintGraphicEx shows BMP as black blocks

Hi Kambiz

Thanks for getting back to me on this - I'm still fighting it and getting frustrated!

This is the code that I am using to insert a BMP onto a TPrintPreview:

Code: Select all
  bmLogo := TBitMap.Create;  try    bmLogo.LoadFromResourceName(HInstance,'FULLLOGO');    PrintBMP(2000,2000,5600,bmLogo);  finally    bmLogo.Free;  end; // tryprocedure PrintBMP(iX,iY : integer;                   iWidth : integer;                   bmPicture : TBitMap); overload;var  iBMPWidth : integer;  iBMPHeight : integer;begin  iBMPWidth := ppOutput^.ConvertX(iWidth,mmHiMetric,ppOutput^.Units);  iBMPHeight := ppOutput^.ConvertX((bmPicture.Height * iWidth) div  bmPicture.Width,mmHiMetric,ppOutput^.Units);  ppOutput^.PaintGraphicEx(Rect(ppOutput^.ConvertX(iX,mmHiMetric,ppOutput^.Units),                                ppOutput^.ConvertY(iY,mmHiMetric,ppOutput^.Units),                                ppOutput^.ConvertX(iX,mmHiMetric,ppOutput^.Units) + iBMPWidth,                                ppOutput^.ConvertY(iY,mmHiMetric,ppOutput^.Units) + iBMPHeight),                           bmPicture,FALSE,FALSE,FALSE);end; // PrintBMP

This code has been in use for years and has never gave any problems (with various versions of TPrintPreview).

Another BMP that I add to the printouts is a bar code which I create on-the-fly by simply painting on a TBitMap's Canvas. Again, I place it on the TPrintPreview component using the above PrintBMP routine. This BMP occasionally also comes out all black in the printouts.

1) I always Free the TBitMap after use (see code above)
2) I am trying with loading from a file (but the fact that an on-the-fly created BMP also has problems, seems to imply that the resource is not necessarily the problem)
3) I do note that the BMP is 24bit and TBitMap.Create by default sets PixelFormat to pf8bit. I will try adjusting this setting.
4) TBitMap.Create by default sets Transparent to False. I have set the BMP itself, (using Paint Shop Pro) to have no transparency.

The users may use the software to generate invoices and other documents for hours without coming across a problem. Then, at other times, they might have a large number of black blocks, as shown in the original image I posted. I have only encountered it once on my development PC.

An issue that I created in July does not have the problem. An attempt to recompile that exe, on the same PC, using all the same components, library units and archived source code creates a slightly different sized exe file, and this does show the problems. I have also compiled the code on a different PC, which produced an exe file that was again different in size to the other two, and this also produced black blocks.

I've attached the company logo BMP that I am putting at the top of all documents.

Andrew
Attachments
TraXLogoAndCoName.zip
AndrewTBDev
Member

Posts: 3
Joined: September 3rd, 2013, 10:09 am

### Re: TPrintPreview.PaintGraphicEx shows BMP as black blocks

Hi Andrew,

I created more than 10,000 pages using you sample image. Also, I left the code running for several hours. In both cases, no problem occurred.

What about to load the image only once and use the cached copy for printing? In that case, it is better after loading the image from resource, call TBitmap.Dormant method to release the GDI resource.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: TPrintPreview.PaintGraphicEx shows BMP as black blocks

Hi Kambiz

Thank you for following up with this and doing the tests. It is very much appreciated.

I have since also attempted a small, quick, multi-page generation program, and not got any problems. However, when I tried embedding that test into my MRP software, the block errors started to appear. Also, it is not the number of times an image is placed on a TPrintPreview, but rather that with repeated generation, errors crop up. I think that this means that I should stop worrying this forum, and look to my own software!

I have tried loading the image once for the session, as you suggested, but that did not solve the problem.

The interesting thing about errors that I recorded about a week ago is that, while a very high proportion of that 10% were black, there were a very few (about 1 in every 30) that were other colours, including some with rows of pixels through them. See the attached PDFs.

Also interestingly, I seem to be having some success in replacing
Code: Select all
  ppOutput^.PaintGraphicEx(Rect(ppOutput^.ConvertX(iX,mmHiMetric,ppOutput^.Units),                                ppOutput^.ConvertY(iY,mmHiMetric,ppOutput^.Units),                                ppOutput^.ConvertX(iX,mmHiMetric,ppOutput^.Units) + iBMPWidth,                                ppOutput^.ConvertY(iY,mmHiMetric,ppOutput^.Units) + iBMPHeight),                           bmPicture,FALSE,FALSE,FALSE);

with
Code: Select all
  ppOutput^.Canvas.StretchDraw(Rect(ppOutput^.ConvertX(iX,mmHiMetric,ppOutput^.Units),                                    ppOutput^.ConvertY(iY,mmHiMetric,ppOutput^.Units),                                    ppOutput^.ConvertX(iX,mmHiMetric,ppOutput^.Units) + iBMPWidth,                                    ppOutput^.ConvertY(iY,mmHiMetric,ppOutput^.Units) + iBMPHeight),                               bmPicture);

For now, I will keep trying, and let you know what I find. Thanks again!

Andrew
Attachments
Blocks.zip
AndrewTBDev
Member

Posts: 3
Joined: September 3rd, 2013, 10:09 am

### Re: TPrintPreview.PaintGraphicEx shows BMP as black blocks

Hi Andrew,

Any GDI resource leak in your application may cause this problem. Although I've tested PrintPreview, but this component may also have a leaking issue.

I had successful experiences in using MemProof for finding memory/resource leaks in my Delphi 7 codes. You should give it a try.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: TPrintPreview.PaintGraphicEx shows BMP as black blocks

Hi Andrew,

The problem could be caused because of a resource leak in the component. Please try the new update.

Here the problem is described: viewtopic.php?f=7&t=2183
Kambiz

Kambiz