Send Image to Printer Topic is solved
Send Image to Printer
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.
Re: Send Image to Printer
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
Re: Send Image to Printer
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?
Re: Send Image to Printer
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)".
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)".
Re: Send Image to Printer
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:
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.
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)
}
Re: Send Image to Printer
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.
Re: Send Image to Printer
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.
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.
Re: Send Image to Printer Topic is solved
Web search results:
https://github.com/Ixiko/AHK-libs-and-classes-collection/blob/master/libs/o-z/SGDIPrint.ahk
https://github.com/marius-sucan/other-small-AHK-scripts/blob/master/sgdiprint.ahk
viewtopic.php?style=7&t=68403
https://github.com/marius-sucan/other-small-AHK-scripts
My Web search term: SGDIPrint.ahk
https://github.com/Ixiko/AHK-libs-and-classes-collection/blob/master/libs/o-z/SGDIPrint.ahk
https://github.com/marius-sucan/other-small-AHK-scripts/blob/master/sgdiprint.ahk
viewtopic.php?style=7&t=68403
https://github.com/marius-sucan/other-small-AHK-scripts
My Web search term: SGDIPrint.ahk
Re: Send Image to Printer
Oh right, I forgot there was a Web beyond this forum! I'll see if I can get that working, maybe tomorrow. Thanks, Mikeyww, you're a star.
Re: Send Image to Printer
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
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!
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
Thanks again!
Re: Send Image to Printer
Could be; I do not know. Thank you for sharing your solution here. I think it will be useful to many readers.