WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Pixie (obsolete)
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Pixie Photo Print Sample


Here's a handy snip of code that will print a image file (bmp, jpg, gif) with an option to to size it to fit to a letter-sized page (8" x 10.5" sized image). I was not sure of the number of pixels I should use to represent an 8 x 10.5 size page. I experimented and found that Internet Explorer used around 96 DPI (768 x 1008 pixels) to print a page. If you know if this is true, or know of a way of determining what DPI should be used, please do post.

You will need the Pixie extender.

Photo Print ;


;***************************************************************************
;** PhotoPrint.wbt - Prints any .jpg, .bmp, or .gif photo to fit a Letter-sized page
;** 
;** Adrian Hayes
;** with content adapted from WinBatch Tech Database
;***************************************************************************


AddExtender("WWIMG34I.dll")



;***************************************************************************
;** Print8x10_5(PicFile, FitToPage)
;** Prints a JPEG, BMP, or GIF file to a Letter sized page.
;**
;** Parameters:
;** (s) PicFile:	Full path to .jpg, .bmp, or .gif file
;** (b) FitToPage: @True or @False to fit (shrink or expand) to page.
;**
;***************************************************************************

#DefineFunction Print8x10_5(PicFile, FitToPage)
	DPI = 96
	OutputW = 8.0
	OutPutH = 10.5

	If !ImgIsValid(PicFile) Then Return 0	;; Check if Pixie can dig it.

	PicFileSize = GetPicSize(PicFile)
	If PicfileSize == 0 Then Return 0		;; Error in the GetPicSize function
	PicFileW = ItemExtract(1, PicFileSize, @TAB)
	PicFileH = ItemExtract(2, PicFileSize, @TAB)
	TmpPicFile = FileCreateTemp("ptf")
	FileDelete(TmpPicFile)
	TmpPicFile = StrCat(FilePath(TmpPicFile), FileRoot(TmpPicFile), ".", FileExtension(PicFile))

	If FitToPage
		If PicFileW > PicFileH	;; Rotate if Width wider than Height for portrait printing
			ImgRotate(PicFile, TmpPicFile, 90.0)
			tempvar = PicFileH
			PicFileH = PicFileW
			PicFileW = tempvar
			Drop(tempvar)
		Else
			FileCopy(PicFile, TmpPicFile, @FALSE)
		EndIf

		If PicFileW * 1.0 / OutputW * DPI * 1.0 < PicFileH * 1.0 / OutputH * DPI * 1.0	;; Adjust to height of page
			NewPicH = Int(OutputH * DPI)
			NewPicW = Int(PicFileW * (NewPicH * 1.0 / PicFileH * 1.0))
		Else						;; Adjust to width of page
			NewPicW = Int(OutputW * DPI)
			NewPicH = Int(PicFileH * (NewPicW * 1.0 / PicFileW * 1.0))
		EndIf
	Else
		NewPicW = PicFileW
		NewPicH = PicFileH
		FileCopy(PicFile, TmpPicFile, @FALSE)
	EndIf

	;; Time to write the cheapie HTML - yeah I know, it's not wwcan
	TmpURL = FileCreateTemp("puf")
	FileDelete(TmpURL)
	TmpURL = StrCat(FilePath(TmpURL), FileRoot(TmpURL), ".htm")
	TmpURLHan = FileOpen(TmpURL, "WRITE")
	FileWrite(TmpURLHan, "")
	FileWrite(TmpURLHan, "")
	FileWrite(TmpURLHan, "")
	FileWrite(TmpURLHan, "")
	FileWrite(TmpURLHan, "")
	FileWrite(TmpURLHan, StrCat(""))
	FileWrite(TmpURLHan, "")
	FileWrite(TmpURLHan, "")
	FileClose(TmpURLHan)

	;; Set all margins in Internet Explorer to 1/4", but remember them to set them back.
	IEPageSetupKey = RegOpenKey(@REGCURRENT, "Software\Microsoft\Internet Explorer\PageSetup")
	CurMargin_Bottom = RegQueryValue(IEPageSetupKey, "[margin_bottom]")
	CurMargin_Left = RegQueryValue(IEPageSetupKey, "[margin_left]")
	CurMargin_Right = RegQueryValue(IEPageSetupKey, "[margin_right]")
	CurMargin_Top = RegQueryValue(IEPageSetupKey, "[margin_top]")

	If CurMargin_Bottom != 0.25 Then RegSetValue(IEPageSetupKey, "[margin_bottom]", "0.25000")
	If CurMargin_Left != 0.25 Then  RegSetValue(IEPageSetupKey, "[margin_left]", "0.25000")
	If CurMargin_Right != 0.25 Then RegSetValue(IEPageSetupKey, "[margin_right]", "0.25000")
	If CurMargin_Top != 0.25 Then RegSetValue(IEPageSetupKey, "[margin_top]" "0.25000")

	BrowserPrint(TmpURL)

	TimeDelay(1)	;; Give it an extra second to tie up any loose ends before deleting the files.

	If CurMargin_Bottom != 0.25 Then RegSetValue(IEPageSetupKey, "[margin_bottom]", CurMargin_Bottom)
	If CurMargin_Left != 0.25 Then  RegSetValue(IEPageSetupKey, "[margin_left]", CurMargin_Left)
	If CurMargin_Right != 0.25 Then RegSetValue(IEPageSetupKey, "[margin_right]", CurMargin_Right)
	If CurMargin_Top != 0.25 Then RegSetValue(IEPageSetupKey, "[margin_top]" CurMargin_Top)

	FileDelete(TmpPicFile)
	FileDelete(TmpURL)
#EndFunction



;***************************************************************************
;** BrowserPrint(File)
;** Prints the specified HTML (or other file type) document.
;** Adapted from WinBatch Tech Database Article W16168
;***************************************************************************

#DefineFunction BrowserPrint(File)
   ;This can be used to Print:
	;HTML files 
	;Graphics files (i.e., BMP TIFF JPG JPEG GIF)
	;PDF Documents (If Acrobat Reader 4 or higher and IE4 or higher are installed)

	If !FileExist(File)
		Message("Error: File Missing", StrCat("Error:  Could not print file: ", @CRLF, File, @CRLF, @CRLF, "The specified file does not exist."))
		Return(0)
	EndIf

	objBrowser = objectopen("InternetExplorer.Application")
	objBrowser.visible = @FALSE
	objBrowser.navigate(file)
	While @true
	   If objBrowser.ReadyState == 4 then Break
	   TimeDelay(.5)
	EndWhile
	;http://msdn.microsoft.com/library/en-us/com/htm/oen_a2z_22sk.asp?frame=true
	OLECMDID_PRINT = 6
	;http://msdn.microsoft.com/library/en-us/com/htm/oen_a2z_5k38.asp?frame=true
	OLECMDEXECOPT_DONTPROMPTUSER  = 2
	;http://msdn.microsoft.com/workshop/browser/mshtml/reference/constants/idm_print.asp
	PRINT_DEFAULT = 0
	PRINT_DONTBOTHERUSER = 1
	PRINT_WAITFORCOMPLETION = 2
	pvaIn = ObjectType("I2", 0)
	pvaOut = ObjectType("NULL","")
	
	;http://msdn.microsoft.com/workshop/browser/webbrowser/reference/ifaces/iwebbrowser2/execwb.asp?frame=true
	objBrowser.ExecWB(OLECMDID_PRINT, 1, pvaIn, pvaOut)

	WinWaitExist("Print", 7)
	If WinExist("Print")
		WinActivate("Print")
	EndIf

	While @TRUE
		If objBrowser.Busy == @FALSE Then Break
		TimeDelay(.5)
	EndWhile

	;Give the browser enough time to print before closing the object
	TimeDelay(1)
	ObjBrowser.Quit()
	ObjectClose(objBrowser)
	Return(1)
#EndFunction



;***************************************************************************
;** GetPicSize(thisfile)
;** Returns the size of 'thisfile' as the width and height separated by a TAB delimiter
;** Adapted from WinBatch Tech Database Article W14954
;***************************************************************************

#DefineFunction GetPicSize(thisfile)
	M_SOF0 =     192; #define M_SOF0  0xC0		/* Start Of Frame N */
	M_SOF1 =     193; #define M_SOF1  0xC1		/* N indicates which compression process */
	M_SOF2 =     194; #define M_SOF2  0xC2		/* Only SOF0-SOF2 are now in common use */
	M_SOF3 =     195; #define M_SOF3  0xC3
	M_SOF5 =     197; #define M_SOF5  0xC5		/* NB: codes C4 and CC are NOT SOF markers */
	M_SOF6 =     198; #define M_SOF6  0xC6
	M_SOF7 =     199; #define M_SOF7  0xC7
	M_SOF9 =     201; #define M_SOF9  0xC9
	M_SOF10=     202; #define M_SOF10 0xCA
	M_SOF11=     203; #define M_SOF11 0xCB
	M_SOF13=     205; #define M_SOF13 0xCD
	M_SOF14=     206; #define M_SOF14 0xCE
	M_SOF15=     207; #define M_SOF15 0xCF
	M_SOI  =     216; #define M_SOI   0xD8		/* Start Of Image (beginning of datastream) */
	M_EOI  =     217; #define M_EOI   0xD9		/* End Of Image (end of datastream) */
	M_SOS  =     218; #define M_SOS   0xDA		/* Start Of Scan (begins compressed data) */
	M_APP0 =     224; #define M_APP0	0xE0		/* Application-specific marker, type N */
	M_APP12=     236; #define M_APP12	0xEC		/* (we don't bother to list all 16 APPn's) */
	M_COM  =     254; #define M_COM   0xFE		/* COMment */

	bb = BinaryAlloc(FileSize(thisfile))
	thisExt = strlower(FileExtension(thisfile))
	thisroot = FileRoot(thisfile)
	BinaryEODSet(bb, 0)
	BinaryRead(bb, thisfile)
	If thisExt == "gif"
		width = BinaryPeek2(bb, 6)
		height = BinaryPeek2(bb, 8)
	Else     ; JPEG
		off = 0
		;Get and check first marker
		c1 = BinaryPeek(bb, off)
		If c1 != 255 Then Return 0				;;Not jpeg.  First byte not FF
		off = off + 1

		c2 = BinaryPeek(bb, off)
		If c2 != M_SOI Then Return 0			;;Not jpeg.  SOI marker missing
		off = off + 1
		marker = c2			;;We just past SOI marker.  Get next marker

		while @TRUE			;Discard bytes till be get a FF byte
			While (BinaryPeek(bb, off) != 255)
			      off = off + 1
			EndWhile
			While BinaryPeek(bb, off) == 255					;Now discard FF bytes till be get a databyte
			    off=off + 1
			EndWhile
			marker = BinaryPeek(bb, off)
			off = off + 1
			
			Switch marker
				case M_SOF0;		/* Baseline */
				case M_SOF1;		/* Extended sequential, Huffman */
				case M_SOF2;		/* Progressive, Huffman */
				case M_SOF3;		/* Lossless, Huffman */
				case M_SOF5;		/* Differential sequential, Huffman */
				case M_SOF6;		/* Differential progressive, Huffman */
				case M_SOF7;		/* Differential lossless, Huffman */
				case M_SOF9;		/* Extended sequential, arithmetic */
				case M_SOF10;		/* Progressive, arithmetic */
				case M_SOF11;		/* Lossless, arithmetic */
				case M_SOF13;		/* Differential sequential, arithmetic */
				case M_SOF14;		/* Differential progressive, arithmetic */
				case M_SOF15;		/* Differential lossless, arithmetic */
					len = BinaryPeek(bb, off) * 256 + BinaryPeek(bb, off + 1)
					datapres = BinaryPeek(bb, off + 2)
					height = BinaryPeek(bb, off + 3) * 256 + BinaryPeek(bb, off + 4)
					width = BinaryPeek(bb, off + 5) * 256 + BinaryPeek(bb, off + 6)
					comp = BinaryPeek(bb, off + 7)	;Message(off,"H=%height% W=%width%")
				   len = BinaryPeek(bb, off) * 256 + BinaryPeek(bb, off + 1)
				   off = off + len
					Break

				case marker;			/* Anything else just gets skipped */
					len = BinaryPeek(bb, off) * 256 + BinaryPeek(bb, off + 1)
					off = off + len
					Break;
			EndSwitch
			If marker == M_SOS Then Break
		EndWhile
	Endif
	BinaryFree(bb)
	Return StrCat(width, @TAB, height)
;; End of GetPicSize Function
#EndFunction







;***************************************************************************
;** Test Code
;** 
;** 
;***************************************************************************

PicFiles = AskFileName("Please select the image file(s) you wish to print.", "", "Image Files|*.jpg;*.jpeg;*.bmp;*.gif", "", 2)

For CurItemNum = 1 to ItemCount(PicFiles, @TAB)
	CurItem = ItemExtract(CurItemNum, PicFiles, @TAB)
	Print8x10_5(CurItem, @TRUE)
Next CurItemNum


Article ID:   W16346
File Created: 2014:01:29:15:34:52
Last Updated: 2014:01:29:15:34:52