Author Topic: Trying to print w/GDI+ -- Help!  (Read 960 times)

NoCforMe

  • Member
  • *****
  • Posts: 1124
Trying to print w/GDI+ -- Help!
« 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!

Code: [Select]
; 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!

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Trying to print w/GDI+ -- Help!
« Reply #1 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?

NoCforMe

  • Member
  • *****
  • Posts: 1124
Re: Trying to print w/GDI+ -- Help!
« Reply #2 on: June 11, 2022, 08:32:48 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():

Code: [Select]
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) ...

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Trying to print w/GDI+ -- Help!
« Reply #3 on: June 11, 2022, 08:38:47 AM »
GdipSetPageUnit() returned 2, but that's probably just the previous "unit" value (I'm guessing, anyhow) ...

Code 2 is InvalidParameter

NoCforMe

  • Member
  • *****
  • Posts: 1124
Re: Trying to print w/GDI+ -- Help!
« Reply #4 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.

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Trying to print w/GDI+ -- Help!
« Reply #5 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.

NoCforMe

  • Member
  • *****
  • Posts: 1124
Re: Trying to print w/GDI+ -- Help!
« Reply #6 on: June 11, 2022, 09:40:38 AM »
Aaaargh. My bad. Passed a pointer to a pointer instead of the pointer itself;
Code: [Select]
INVOKE GdipSetPageUnit, gdiHgraphics, 2
fixed that. Still doesn't print anything, though.

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Trying to print w/GDI+ -- Help!
« Reply #7 on: June 11, 2022, 09:51:25 AM »
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.)

If nothing helps, you might try GdipCreateHBITMAPFromBitmap - it gives you a GDI bitmap from a GDI+ bitmap.

NoCforMe

  • Member
  • *****
  • Posts: 1124
Re: Trying to print w/GDI+ -- Help!
« Reply #8 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 ...

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Trying to print w/GDI+ -- Help!
« Reply #9 on: June 11, 2022, 10:33:09 AM »
Wow, excellent, you are fast :thumbsup:

NoCforMe

  • Member
  • *****
  • Posts: 1124
Re: Trying to print w/GDI+ -- Help!
« Reply #10 on: June 11, 2022, 10:50:31 AM »
Just bored and nothing else to do at the moment ...