Page 1 of 1

A program which saves pictures from a TWAIN source/Clipboard

Posted: Tue Feb 03, 2009 10:50 pm
by infratec
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 :D I need only a few lines of PureBASIC:

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
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

Posted: Wed Feb 04, 2009 12:31 am
by Fangbeast
That's pretty damned amazing and incredibly useful. I can think of a load of uses for this. Thank you.

Posted: Wed Feb 04, 2009 9:01 am
by infratec
Thanks Fangbeast,

thats a small tribute to all members of this forum!
All my (sometimes unecessary) questions were answered and helped me to come forward with PB.
So I want to give something back :D

!!! Bug fixed !!!

When you first have a too big picture, than a to small picture and than again
a big picture, the picture was wrong resized on the screen.

This is fixed now in the listing.


Bernd

Posted: Wed Feb 04, 2009 11:08 am
by infratec
Hi together,

I edit the sourcecode again.
One of our PCs had several TWAIN drivers.
So a select was neccessary on that PC.

With /s as parameter now the select dialog appears.
/? shows a small usage message

Bernd

Posted: Wed Feb 04, 2009 12:02 pm
by Fangbeast
infratec wrote:Hi together,

I edit the sourcecode again.
One of our PCs had several TWAIN drivers.
So a select was neccessary on that PC.

With /s as parameter now the select dialog appears.
/? shows a small usage message

Bernd
Works perfectly here Bernd, thank you.

Posted: Wed Feb 04, 2009 12:56 pm
by infratec
Again small extensions:

Now you can easy customize the program:

Simply change the #TITLE and #VERSION constants

If you have now the TwScann.dll not installed you are informed that
you can only grap pictures from the clipboard and save them.

Have fun with it. :wink:

Bernd

Posted: Thu Apr 16, 2009 5:50 am
by Fangbeast
Bern, playing with this code again and had a couple of questions.

Is there a way to hide the scanner driver and just show the picture window and a way to get a progress report?

Is there any way to scan directly to the picture window? I have seen some programs do this, don't know how.

Posted: Sat Apr 18, 2009 11:33 am
by infratec
Hi Fangbeast,

sorry for the delay, but I was in a foreign country without access to my source code.

In general it is possible.

But it depends on the TWAIN driver and the dll which supports it.

I also tried the EZ Twain solution.

http://www.dosadi.com/products.htm

There are functions available like:

TWAIN_SetHideUI()
TWAIN_AcquireNative()

(Also in the freeware version)

But not all TWAIN drivers have all features implemented.

So it is a kind of try and error.



I just tested TWAIN_SetHideUI().
Result with my WebCam driver:

The TWAIN interface apears shortly, closes immediately and I have the picture in my program.
Without SetHideUI the interface stays open until I press the snapshot button.


Best regards,

Bernd

Posted: Sun Apr 19, 2009 12:55 am
by Fangbeast
I was afraid that might be the case. Thanks Bernd.

It's fun playing.