Send Image to Printer Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Send Image to Printer

Post by ahketype » 18 Jan 2022, 19:28

Hi, after creating a bitmap image on the screen with GDI+, I'd like to send it to a printer. Assuming that can be done, could anyone give me an example script, please? I'm not sure if I can use a function in Gdip.ahk or what. If it was possible to do this invisibly, that would be ideal, and if several images could be queued as pages to print, I'd be over the moon. Cheers.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Send Image to Printer

Post by mikeyww » 18 Jan 2022, 19:40

Code: Select all

#Include d:\Q\vis2\lib\Gdip_All.ahk
out   = %A_ScriptDir%\test.png
print = d:\utils\irfanView64\i_view64.exe

; F3::
pToken := Gdip_Startup(), snap := Gdip_BitmapFromScreen(100 "|" 100 "|" 300 "|" 300)
Gdip_SaveBitmapToFile(snap, out), Gdip_DisposeImage(snap), Gdip_Shutdown(pToken)
RunWait, %print% "%out%" /print
Return

ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Send Image to Printer

Post by ahketype » 18 Jan 2022, 19:56

Thanks, Mikey. So this is saving the file from the screen, and then what - using Irfanview to print it? I was hoping for something without that kind of dependence on another application. Is there another way?

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Send Image to Printer

Post by mikeyww » 18 Jan 2022, 20:04

Yes. I suppose there could be some way to create a PostScript file and deliver it directly to a device-- beyond my knowledge. You could search the forum here. I have not seen any recent posts like that.

This "PDF to printer" page might have some leads, links to source code, etc.

http://www.columbia.edu/~em36/pdftoprinter.html

Also: https://www.autohotkey.com/board/topic/76258-ahk-llibsimple-gdi-printing-library/

Has some functions like "SGDIPrint_CopyBitmapToPrinterHDC(pBitmap, hDC)".

ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Send Image to Printer

Post by ahketype » 19 Jan 2022, 04:54

Those look like they might be good. I also notice there are a couple of possible functions in Gdip.ahk, like this, but I'd have to play about / learn more:

Code: Select all

; Function				PrintWindow
; Description			The PrintWindow function copies a visual window into the specified device context (DC), typically a printer DC
;
; hwnd					A handle to the window that will be copied
; hdc					A handle to the device context
; Flags					Drawing options
;
; return				If the function succeeds, it returns a nonzero value
;
; PW_CLIENTONLY			= 1

PrintWindow(hwnd, hdc, Flags=0)
{
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	
	return DllCall("PrintWindow", Ptr, hwnd, Ptr, hdc, "uint", Flags)
}
I'm not sure whether copying the visual window "into the specified device context (DC)" is just one step - maybe I have to find the printer DC first, and then maybe do something after this to actually print it.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Send Image to Printer

Post by mikeyww » 19 Jan 2022, 06:24

Searching the forum for "Gdip PrintWindow" shows a bunch of posts, possibly some leads. In addition, the AHK page that I cited above has an example of getting an hDC value for a printer. That's about all I know, never tried it.

ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Send Image to Printer

Post by ahketype » 19 Jan 2022, 18:47

mikeyww wrote:
19 Jan 2022, 06:24
Searching the forum for "Gdip PrintWindow" shows a bunch of posts, possibly some leads. In addition, the AHK page that I cited above has an example of getting an hDC value for a printer. That's about all I know, never tried it.
I can't find SGDIPrint.ahk - any idea where it is / who might have it? Zed Gecko gives examples of its use, including getting an hDC for a printer, but the library seems to have been deleted: "code removed due to protest". The only other hits for "SGDIPrint.ahk" seem to be in lost code archives. Zed Gecko doesn't appear to be in the member list either. :problem:

Maybe MSDN will throw some light on how to find the printer hDC and then maybe the Gdip.ahk function PrintWindow might do the business.


ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Send Image to Printer

Post by ahketype » 19 Jan 2022, 19:30

Oh right, I forgot there was a Web beyond this forum! :lol: I'll see if I can get that working, maybe tomorrow. :yawn: Thanks, Mikeyww, you're a star.

ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Send Image to Printer

Post by ahketype » 28 Feb 2022, 09:06

I've just set this to "solved" with SGDIPrint.ahk as you suggested, @mikeyww, and I thought I'd share a method that (kind of) worked for me. The situation I've got is a page of graphics already printed on screen, drawn using GDIP.ahk, with which the user can interact. I just wanted them to be able to hit Ctrl-P or whatever and print it.

As per original examples, I used the graphics pointer, G and the bitmap pointer pBitmap for drawing that image. I found I could copy the image already on screen to the printer hDC, and here I avoid the clash of variables by using G1 for the printer graphics.

The following is just the additional hotkey for the print routine. Gdip.ahk and SGDIPrint.ahk are in my default library, and GDIP has already been started. The routine was adapted from the examples at viewtopic.php?style=7&t=68403

Code: Select all

^p::
pPrinterName := SGDIPrint_GetDefaultPrinter()
hdc := SGDIPrint_GetHDCfromPrinterName(pPrinterName,2,0,1)
SGDIPrint_BeginDocument(hDC, "print test")
G1 := SGDIPrint_printerfriendlyGraphicsFromHDC(hDC)
SGDIPrint_CopyBitmapToPrinterHDC(pBitmap, hDC)
SGDIPrint_DeleteGraphics(G1)
SGDIPrint_EndDocument(hDC)
return
The problem I have with this is that the Landscape instruction - the 2 in hdc := SGDIPrint_GetHDCfromPrinterName(pPrinterName,2,0,1) - is ignored (or appears to be). That might simply be because it doesn't apply to the image copied onto the printer hDC, which would need to be rotated first for that. In fact, that was just a test - I want the page in Portrait, but the image is distorted (because it's filling the Landscape orientation of my laptop screen). I guess this is because I haven't taken account of the printer's dimension settings.
Thanks again!

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Send Image to Printer

Post by mikeyww » 28 Feb 2022, 09:22

Could be; I do not know. Thank you for sharing your solution here. I think it will be useful to many readers.

Post Reply

Return to “Ask for Help (v1)”