Jump to content
Sign in to follow this  

Apply ICC Profile when printing with GDI+ and using PrintWinAPI

Recommended Posts


Hello All,

I've been looking at using PrintWinAPI.au3 to print images.

Many thanks to the excellent thread started by GRS (link below) which got me started.

My issue is when I print, any device ICC assigned to the printer in windows is not used?!

I'm starting to believe I need to apply the ICC to the image before sending to the printer.

The following article suggest to me that it's possible but not simple: http://support.microsoft.com/kb/317825

Has anyone looked into this before? I can't find any existing thread that covers this.

Many thanks

Below is the print function I'm using for testing; you need to change the printer name.

#Include <PrintWinAPI.au3>

Local $sPrinterName = "Printer Name as found in 'Devices and Printers'"; edit accordingly
Local $sfilePath = FileOpenDialog("Select an image", "", "Images (*.jpg;*.png;*.bmp;*.gif;*.tif)", 3)

_PrintImage($sfilePath, $sPrinterName,1)

Func _PrintImage($sfile, $sPrinterName, $iResize = 2, $autoRotate = True)

Local $hPrintDC, $hImage
Local $jobInc = 0

If Not FileExists($sfile) Then Return

$hPrintDC = _WinAPI_CreateDC("winspool", $sPrinterName)
$dividerH = _WinAPI_GetDeviceCaps($hPrintDC, $__WINAPICONSTANT_LOGPIXELSY)/100; not sure if I have these the right way round
$dividerW = _WinAPI_GetDeviceCaps($hPrintDC, $__WINAPICONSTANT_LOGPIXELSX)/100
$PageWidth = _WinAPI_GetDeviceCaps($hPrintDC, $HORZRES); Get width, in millimeters, of the physical page
$PageHeight = _WinAPI_GetDeviceCaps($hPrintDC, $VERTRES); Get height, in millimeters, of the physical page
$PageRatio = $PageHeight/$PageWidth

; setup docinfo
$s_DocName = "Test_" & $jobInc
$DocName = DllStructCreate("char DocName[" & StringLen($s_DocName & chr(0)) & "]")
DllStructSetData($DocName, "DocName", $s_DocName & chr(0)); Size of DOCINFO structure
$DOCINFO = DllStructCreate($tagDOCINFO); Structure for Print Document info
DllStructSetData($DOCINFO, "Size", 20); Size of DOCINFO structure
DllStructSetData($DOCINFO, "DocName", DllStructGetPtr($DocName)); Set name of print job (Optional)

;Start the Document
$result = _WinAPI_StartDoc($hPrintDC, $DOCINFO)

$hImage = _GDIPlus_ImageLoadFromFile($sfile)

If $hImage = 0 Or $hImage = -1 Then Return ; Skip if Image file not loaded
$result += _WinAPI_StartPage($hPrintDc)

$iImageW = _GDIPlus_ImageGetWidth($hImage)
$iImageH = _GDIPlus_ImageGetHeight($hImage)
$iImageRatio = $iImageH/$iImageW
If $autoRotate Then
$rotate = False
If $PageRatio > 1 and $iImageRatio < 1 Then
$rotate = True
ElseIf $PageRatio < 1 and $iImageRatio > 1 Then
$rotate = True

If $rotate Then
DllCall($ghGDIPDll, "int", "GdipImageRotateFlip", "hwnd", $hImage, "long", 1); rotate 270 degrees
$iImageW = _GDIPlus_ImageGetWidth($hImage)
$iImageH = _GDIPlus_ImageGetHeight($hImage)
$iImageRatio = $iImageH/$iImageW

If $iResize = 0 Then ; 2 = fill (Default)
$ImageResizeRatio = 1
ElseIf $iResize = 1 Then ; 1 = fit
If $PageRatio > $iImageRatio Then; Image more square than paper - resize to fit height (Potrait!!!)
$ImageResizeRatio = $iImageW/$PageWidth
$ImageResizeRatio = $iImageH/$PageHeight
Else ; fill (Default
If $PageRatio > $iImageRatio Then; Image more square than paper - resize to fit height (Potrait!!!)
$ImageResizeRatio = $iImageH/$PageHeight

$ImageResizeRatio = $iImageW/$PageWidth

$w = Int($iImageW/$ImageResizeRatio)
$h = Int($iImageH/$ImageResizeRatio)
$ImageOffsetH = Int(($PageHeight - $h)/2)
$ImageOffsetW = Int(($PageWidth - $w)/2)

$hGraphic = _GDIPlus_GraphicsCreateFromHDC($hPrintDC)
_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage, $ImageOffsetW/$dividerW, $ImageOffsetH/$dividerH, $w/$dividerW, $h/$dividerH)

$result += _WinAPI_EndPage($hPrintDC)
$result = _WinAPI_EndDoc($hPrintDC)

_GDIPlus_GraphicsDispose ($hGraphic)
_GDIPlus_Shutdown ()


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this