How to scan image in Delphi?

How can I scan a 300dpi image and save in TIFF format using Delphi?

  • I'm using Delphitwain (delphitwain.sourceforge.net) to add scan functionality to my app. Scanning is working fine and I can save bmp and jpeg files. Now I need to: save in 300dpi (the scanner is capable) save in TIFF format After digging around, I found 2 tips: http://www.delphipraxis.net/132787-farbstich-nach-bitmap-operation.html http://synopse.info/fossil/wiki?name=GDI%2B Here is my final code: procedure TForm1.GoAcquireClick(Sender: TObject); begin Counter := 0; Twain.SourceManagerLoaded := TRUE; Twain.LoadSourceManager; Twain.TransferMode := ttmMemory; with Twain.Source[ 0 ] do begin Loaded := TRUE; SetIXResolution(300); SetIYResolution(300); SetIBitDepth(1); EnableSource(true, true); while Enabled do Application.ProcessMessages; end; end; procedure TForm1.TwainTwainAcquire(Sender: TObject; const Index: Integer; Image: TBitmap; var Cancel: Boolean); var TiffHolder: TSynPicture; begin Inc( Counter ); Current := Counter; ImageHolder.Picture.Assign( Image ); ImageHolder.Picture.Bitmap.Monochrome := true; ImageHolder.Picture.Bitmap.Pixelformat := pf1Bit; SynGDIPlus.SaveAs(ImageHolder.Picture, format('c:\temp\teste%d.tif',[ Counter ]), gptTIF ); end; Result: the image still is 96dpi and were saved as BMP (even with TIF extension). What am I missing?

  • Answer:

    Saving TIFF files is hard in Delphi. I don't know any free/open source modules that do this. http://www.xequte.com/imageen/info/index.html works. In the past I have used saving as bmp and converting to tiff with irfanview through createprocess with the commandline 'i_view32.exe c:\temp\scanned.bmp /bpp=1 /convert=c:\temp\scanned.tif'

Josir at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

The GDI+ library is able to save tiff pictures. The SynGdiPlus unit use {557CF405-1A04-11D3-9A73-0000F81EF32E} for its TIFF encoder. From the TSynPicture.SaveAs code, I see two possibilities: Either you don't have the corresponding TIFF encoder installed; Either there is some missing parameter expected by the TIFF encoder. Try this version: type /// the optional TIFF compression levels // - use e.g. ord(evCompressionCCITT4) to save a TIFF picture as CCITT4 TGDIPPEncoderValue = ( evColorTypeCMYK, evColorTypeYCCK, evCompressionLZW, evCompressionCCITT3, evCompressionCCITT4, evCompressionRle, evCompressionNone, (...) evFrameDimensionPage); const EncoderCompression: TGUID = '{e09d739d-ccd4-44ee-8eba-3fbf8be4fc58}'; function TSynPicture.SaveAs(Stream: TStream; Format: TGDIPPictureType; CompressionQuality: integer): TGdipStatus; var fStream: IStream; Len,Dummy: Int64; tmp: pointer; Params: TEncoderParameters; PParams: pointer; MS: TMemoryStream absolute Stream; begin if not Gdip.Exists or (Stream=nil) or (fImage=0) then begin result := stInvalidParameter; exit; end; Params.Count := 1; Params.Parameter[0].Type_ := EncoderParameterValueTypeLong; Params.Parameter[0].NumberOfValues := 1; Params.Parameter[0].Value := @CompressionQuality; PParams := nil; case Format of gptJPG: if CompressionQuality>=0 then begin Params.Parameter[0].Guid := EncoderQuality; PParams := @Params; end; gptTIF: begin if not (TGDIPPEncoderValue(CompressionQuality) in [ evCompressionLZW, evCompressionCCITT3, evCompressionCCITT4, evCompressionRle, evCompressionNone]) then // default tiff compression is LZW CompressionQuality := ord(evCompressionLZW); Params.Parameter[0].Guid := EncoderCompression; PParams := @Params; end; end; CreateStreamOnHGlobal(0, true, fStream); (...) It will add the EncoderCompression parameter for TIFF pictures, which http://msdn.microsoft.com/en-us/library/ms533839%28VS.85%29.aspx. I've updated the source code repository version to include this correction.

Arnaud Bouchez

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.