A program which saves pictures from a TWAIN source/Clipboard
Posted: Tue Feb 03, 2009 10:50 pm
Hi together,
we needed a small and easy solution to save pictures from a special mikroscope cam.
The cam has a twain driver and so we tried several things, but it was all
to complicated.
Than I found TwScann.dll from Andreas Miethe
http://www.ampsoft.eu/purebasic/dlls.php
So the way was clear
I need only a few lines of PureBASIC:
The TwScann lines are comments, so you can test it without a real twain source.
Put simply a picture in the clipboard.
I know that this is only a small and easy program, but maybe it saves someone a few hours of work.
Bernd
we needed a small and easy solution to save pictures from a special mikroscope cam.
The cam has a twain driver and so we tried several things, but it was all
to complicated.
Than I found TwScann.dll from Andreas Miethe
http://www.ampsoft.eu/purebasic/dlls.php
So the way was clear

Code: Select all
; To customize the program,
; simply adjust the following constants
#TITLE = "Scan"
#VERSION = "1.02"
; The following parameters represents the size of the picture preview
; not the over all size of the program window
; But this is only for viewing.
; The image is saved in the original scanned size
#WIDTH = 800
#HEIGHT = 600
#BACKCOLOUR = $000000
SelectTwainSource = #False
For i = 1 To CountProgramParameters()
P$ = ProgramParameter()
If FindString(P$, "/?", 1) = 1
Msg$ = #TITLE + " V" + #VERSION + Chr(13)
Msg$ + Chr(13)
Msg$ + "Usage: " + #TITLE + " [/?][/s]" + Chr(13)
Msg$ + Chr(13)
Msg$ + "/? : shows this help" + Chr(13)
Msg$ + "/s : Select the TWAIN source" + Chr(13)
MessageRequester( #TITLE + " Help", Msg$)
End 1
EndIf
If FindString(P$, "/s", 1) = 1
SelectTwainSource = #True
EndIf
Next i
If OpenWindow(#PB_Any, 0, 0, #WIDTH + 20, #HEIGHT + 60, #TITLE + " V" + #VERSION, #PB_Window_SystemMenu)
ButtonGadget(0, 10, #HEIGHT + 30, 100, 20, "Scan")
ButtonGadget(1, 120, #HEIGHT + 30, 100, 20, "Save")
CreateImage(0, #WIDTH, #HEIGHT)
If StartDrawing(ImageOutput(0))
Box(0, 0, #WIDTH, #HEIGHT, #BACKCOLOUR)
StopDrawing()
EndIf
ImageGadget(2, 10, 10, #WIDTH, #HEIGHT, ImageID(0), #PB_Image_Border )
TwScan = OpenLibrary(0,"TwScann.dll")
If TwScan
Twain = CallFunction(0,"IsTwain")
If Twain = 0
Msg$ = " This PC has no TWAIN interface!" + Chr(13)
Msg$ + Chr(13)
Msg$ + "So only clipboard pictures can be saved." + Chr(13)
MessageRequester(#TITLE + " Info", Msg$)
EndIf
If Twain And SelectTwainSource
CallFunction(0,"SelectSource")
EndIf
Else
Msg$ = " TwScann.dll not found!" + Chr(13)
Msg$ + Chr(13)
Msg$ + "So only clipboard pictures can be saved." + Chr(13)
MessageRequester(#TITLE + " Info", Msg$)
EndIf
UseJPEGImageEncoder()
Exit = #False
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 0:
If TwScan And Twain: CallFunction(0,"ScanToClip",0) : EndIf
Clip = GetClipboardImage(#PB_Any);, #PB_Image_DisplayFormat)
If Clip
Width = ImageWidth(Clip)
Height = ImageHeight(Clip)
If StartDrawing(ImageOutput(0))
Box(0, 0, #WIDTH, #HEIGHT, #BACKCOLOUR)
RatioW.f = 1.0
RatioH.f = 1.0
If Width > #WIDTH
RatioW = Width / #WIDTH
Else
NewWidth = Width
EndIf
If Height > #HEIGHT
RatioH = Height / #HEIGHT
Else
NewHeight = Height
EndIf
If RatioW <> RatioH
If RatioW > RatioH
NewWidth = #WIDTH
NewHeight = Height / RatioW
Else
NewWidth = Width / RatioH
NewHeight = #HEIGHT
EndIf
Else
NewWidth = Width / RatioW
NewHeight = Height / RatioH
EndIf
;MessageRequester("Info", "W:" + Str(Width) + " H:" + Str(Height) + "NW:" + Str(NewWidth) + " NH:" + Str(NewHeight))
DrawImage(ImageID(Clip), (#WIDTH - NewWidth) / 2, (#HEIGHT - NewHeight) / 2, NewWidth, NewHeight)
StopDrawing()
SetGadgetState(2, ImageID(0))
EndIf
Else
MessageRequester(#TITLE + " Info", "No picture in clipboard!")
EndIf
Case 1:
If Clip
Filename$ = SaveFileRequester("Save the picture", "", "JPEG (*.jpg)|*.jpg", 0)
If Filename$
If FindString(Filename$, ".jpg", 1) = 0 : Filename$ + ".jpg" : EndIf
SaveImage(Clip, Filename$, #PB_ImagePlugin_JPEG)
EndIf
Else
MessageRequester(#TITLE + " Info", "No picture available For saving!")
EndIf
EndSelect
EndIf
If EventID = #PB_Event_CloseWindow : Exit = #True : EndIf
Until Exit
If TwScan : CloseLibrary(0) : EndIf
EndIf
Put simply a picture in the clipboard.
I know that this is only a small and easy program, but maybe it saves someone a few hours of work.
Bernd