The MASM Forum

General => The Workshop => Topic started by: NoCforMe on June 11, 2022, 07:19:05 AM

Title: Trying to print w/GDI+ -- Help!
Post by: NoCforMe on June 11, 2022, 07:19:05 AM
I'm stuck. I'm trying to print a page using GDI+. I modified some code that works using GDI+ to display an image in a window. When I run this, I get a completely blank page out of the printer.

Here's the code, at least the relevant parts, that I'm using. Again, the non-printing parts of this code work using a window instead of a printer. I don't want to post the complete code just now; it's complicated, and I'm hoping that someone will be able to point out that I left something out here, or some other (hopefully) simple mistake I'm making.

Also, the printing stuff works; copied from another program that successfully prints a page using GDI functions. it's GDI+ that ain't working here.

I thought that printing to a printer's DC using GDI+ would be similar to using a window's DC, but apparently that may not be the case.

BTW, in case the question comes up, the reason I'm using GDI+ instead of plain old GDI is that I need to be able to use non-BMP image files, which are the only ones you can load with GDI. (Using JPEGs here.)

Thanks in advance to anyone who can throw some light on this problem!


; Use Print dialog to select printer, get handle to DC:
INVOKE PrintDlgEx, ADDR printDlgStruct

INVOKE StartDoc, printDlgStruct.hDC, ADDR docInfo
INVOKE StartPage, printDlgStruct.hDC

; Make image filename Unicode:
INVOKE MakeUnicodeName, OFFSET ImageFileName, ADDR unicodeName

; Open JPEG image file:
INVOKE GdipLoadImageFromFile, ADDR unicodeName, OFFSET GdiHbitmap

; Get width & height of bitmap:
INVOKE GdipGetImageWidth, GdiHbitmap, ADDR imgStruct.imgW
INVOKE GdipGetImageHeight, GdiHbitmap, ADDR imgStruct.imgH

; Set image X- and Y-offsets:
MOV ImgStruct.imgX, 0
MOV ImgStruct.imgY, 0

; Get graphics "object" from DC handle:
INVOKE GdipCreateFromHDC, printDlgStruct.hDC, ADDR gdiHgraphics

; Display image at (X,Y) with dimensions (W,H):
; Many thanks to "mabdelouahab" from the MASM32 forum for this.
INVOKE GdipDrawImageRectI, gdiHgraphics, GdiHbitmap,
imgStruct.imgX, imgStruct.imgY, imgStruct.imgW, imgStruct.imgH

; DOESN'T WORK!
Title: Re: Trying to print w/GDI+ -- Help!
Post by: jj2007 on June 11, 2022, 07:37:01 AM
Printing from a richedit works fine for me, but I use plain GDI, so that won't help you. A quick search says GdipSetPageUnit could be an issue, but it's difficult to see your problem without full code.

I see no error checking. What do the various GDI+ calls return? All zero?
Title: Re: Trying to print w/GDI+ -- Help!
Post by: NoCforMe on June 11, 2022, 08:32:48 AM
Quote from: jj2007 on June 11, 2022, 07:37:01 AM
I see no error checking. What do the various GDI+ calls return? All zero?

Checked returns, at least from GdipLoadImageFromFile(), GdipCreateFromHDC(), and GdipDrawImageRectI(): all return 0.

I put in a call to GdipSetPageUnit():


INVOKE GdipSetPageUnit, ADDR gdiHgraphics, 2


but still no joy. (I got the value 2 from the enumeration list; it means "unitPixel".)

Hmmm; GdipSetPageUnit() returned 2, but that's probably just the previous "unit" value (I'm guessing, anyhow) ...
Title: Re: Trying to print w/GDI+ -- Help!
Post by: jj2007 on June 11, 2022, 08:38:47 AM
Quote from: NoCforMe on June 11, 2022, 08:32:48 AMGdipSetPageUnit() returned 2, but that's probably just the previous "unit" value (I'm guessing, anyhow) ...

Code 2 is InvalidParameter
Title: Re: Trying to print w/GDI+ -- Help!
Post by: NoCforMe on June 11, 2022, 08:52:58 AM
Yes, but is that what that function returns? There are a lot of Win32 functions that set values which return the previous value instead of a status value.
Title: Re: Trying to print w/GDI+ -- Help!
Post by: jj2007 on June 11, 2022, 09:01:56 AM
It's GDI+, a different animal. All GdiPlus functions return 0 for success, and non-zero means an error.
Title: Re: Trying to print w/GDI+ -- Help!
Post by: NoCforMe on June 11, 2022, 09:40:38 AM
Aaaargh. My bad. Passed a pointer to a pointer instead of the pointer itself;

INVOKE GdipSetPageUnit, gdiHgraphics, 2

fixed that. Still doesn't print anything, though.
Title: Re: Trying to print w/GDI+ -- Help!
Post by: jj2007 on June 11, 2022, 09:51:25 AM
Quote from: NoCforMe on June 11, 2022, 07:19:05 AMthe reason I'm using GDI+ instead of plain old GDI is that I need to be able to use non-BMP image files, which are the only ones you can load with GDI. (Using JPEGs here.)

If nothing helps, you might try GdipCreateHBITMAPFromBitmap - it gives you a GDI bitmap from a GDI+ bitmap.
Title: Re: Trying to print w/GDI+ -- Help!
Post by: NoCforMe on June 11, 2022, 10:30:41 AM
That works. Thanks! Used GDI (BitBlt() ) to print and it works. That'll get me through this project.
Still disappointed that I couldn't make it work through GDI+. Last time I got some advice from "mabdelouahab" that solved my problem. Maybe by invoking his name he'll come riding in here on his white horse and give me the magic incantation I need ...
Title: Re: Trying to print w/GDI+ -- Help!
Post by: jj2007 on June 11, 2022, 10:33:09 AM
Wow, excellent, you are fast :thumbsup:
Title: Re: Trying to print w/GDI+ -- Help!
Post by: NoCforMe on June 11, 2022, 10:50:31 AM
Just bored and nothing else to do at the moment ...