#1 2012-07-23 14:31:45

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Draw image and save as tiff

I need some advice.

I have develop a small component that use a TPaintBox, I use the TPainBox to draw on canvas some objects (line, point, circle, ...).

Now I need save my canvas as a TIFF with specific size and resolution (200 dpi) if possible with antialiased.

I'd like know if and how can I do it with your framework. Is it possible? It there a piace of code about it? Which is the more easy way to not rewrite all my code?

Thanks

Offline

#2 2012-07-24 07:09:03

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,132
Website

Re: Draw image and save as tiff

Just search for "TIFF" in the forum and you'll find out your way.

Basically:
- Create a TMetaFile with the expected size;
- Draw directly on it using an associated TMetaFileCanvas instance (do not draw from a TPaintBox bitmap, but you should better share the code for the drawing - e.g. making a sub-method taking a TCanvas as a parameter, which may be either a TPaintBox canvas, either a TMetaFileCanvas);
- Play the metafile using SynGdiPlus to render it into a bitmap;
- Save the bitmap into TIFF using SynGdiPlus.

As an alternative, you may use SynGDIPlus to make all the drawing instead of writing directly to the TPaintBox canvas: all your drawings would be anti-aliased and would have much better rendering result. Just draw the rendered bitmap in TPaintBox.Draw (letting the bitmap be cached for better speed).

Offline

#3 2012-07-25 20:32:47

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Draw image and save as tiff

OK.

For example I want create a 10x11 inch image with 100 dpi.

I can use:

var Bmp: TBitmap;
    MF: TMetaFile;
    MetafileCanvas: TMetafileCanvas;
begin
  Gdip := TGDIPlusFull.Create('gdiplus.dll');
  MF := TMetaFile.Create;

  MF.Width := 1000;
  MF.Height := 1100;

  MetafileCanvas := TMetafileCanvas.Create(MF, 0);
  MetafileCanvas.Brush.Color := clRed;
  MetafileCanvas.Brush.Style := bsDiagCross;
  MetafileCanvas.Ellipse(50, 50, 300 - 50, 200 - 50);
  MetafileCanvas.Free;

  Bmp := Gdip.DrawAntiAliased(MF);

  Image1.Picture.Assign(Bmp);
  SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF); 
  Bmp.Free;

  MF.Free;
  FreeAndNil(GdiP);
end;

In this way the TIFF have always 96 DPI (screen resolution). How can I change it?

Thanks

Last edited by array81 (2012-07-30 17:24:32)

Offline

#4 2012-07-30 17:25:33

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Draw image and save as tiff

I have check the code. I think in this moment is not possible set DPI.
Is it possible add this feature?

Offline

#5 2012-08-02 15:42:56

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,132
Website

Re: Draw image and save as tiff

See http://synopse.info/fossil/info/5850eaeb4b

Should be able to set the DPI, as such:

Bmp := Gdip.DrawAntiAliased(MF);
Image1.Picture.Assign(Bmp);
SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF, 80, 0, 200); // force 200 DPI
Bmp.Free;

Offline

#6 2012-08-02 22:41:06

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Draw image and save as tiff

Many thanks.

Other question smile

Is there a way to set color depth? I need of a tiff with 1bit of color (only black and with, no gray scale). Is it possible?

Thanks again.

Last edited by array81 (2012-08-02 22:43:55)

Offline

#7 2012-08-03 05:08:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,132
Website

Re: Draw image and save as tiff

You can set the Bmp to be black and white, as such:

Bmp.Monochrome := true;

But the default GDI implementation is very rough and rude.
Result may probably be not aesthetically correct at all.
You'll loose all anti-aliasing features of GDI+, which, like every anti-aliasing, use half tones.

You would need a sub-sample algorithm, like Floyd-Steinberg.
But it is out of the scope of this library, I'm afraid!

Offline

#8 2012-08-11 12:56:03

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Draw image and save as tiff

I have solved using ord(evCompressionCCITT4) on SynGDIPlus.SaveAs.
Thanks

Offline

#9 2012-08-21 19:05:10

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Draw image and save as tiff

I don't understand the minimum to use the SynGDIPlus.
I have tested my code on Windows 7 without problem, but on Windows XP I get a TIF image of 0 kb...

Which OS I need? Is there a way to extend the use?

Offline

#10 2012-08-21 19:36:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,132
Website

Re: Draw image and save as tiff

For TIFF support, it will depend on the image WIC drivers available in the OS.

I suspect you will find you answer on Google.
See for instance http://msdn.microsoft.com/en-us/library … 85%29.aspx

Perhaps the WIC update would help: http://www.microsoft.com/en-us/download … aspx?id=32

Offline

#11 2012-08-23 14:48:07

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Draw image and save as tiff

My searches have not led to anything.
I have download Windows Imaging Component but when I try to install it I get an error because the last version is just installed on my Windows XP.
I have also copy gdipluss.dll library on my application path, but also this not work.

I don't have an error, only a TIFF file empty...

Other ideas about it?

Offline

#12 2012-08-23 17:09:19

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,132
Website

Re: Draw image and save as tiff

Check the gdiplus.dll which is loaded.
There are several versions around.

During intialization of the object, it may use the one supplied with Office.

TIFF is definitively not the best format when using the GDI+ library.
sad

Offline

#13 2012-08-23 17:21:53

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Draw image and save as tiff

I will check gdiplus.dll and the I will report the result.

I need to create a TIFF from a BMP with 200dpi and evCompressionCCITT4, do you know other delphi class or small library for this?

Offline

#14 2012-09-14 23:35:54

trakless
Member
Registered: 2012-09-14
Posts: 3

Re: Draw image and save as tiff

I've been trying to automate saving to 300dpi Tif's for ages and noticed AB's code snippet of 02/08/12 -

Bmp := Gdip.DrawAntiAliased(MF);
Image1.Picture.Assign(Bmp);
SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF, 80, 0, 200); // force 200 DPI
Bmp.Free;

Just that I get 'Too many actual parameters'. Knocking off the 200 creates a Tif OK, but the very parameter I need seems to be unavailable.

Any ideas gratefully etc.

Offline

#15 2012-09-15 19:54:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,132
Website

Re: Draw image and save as tiff

did you get the latest version of the unit?

Offline

#16 2012-09-15 22:16:31

trakless
Member
Registered: 2012-09-14
Posts: 3

Re: Draw image and save as tiff

I downloaded SynGdiPlus.zip on 12/9/12 via http://synopse.info/fossil/wiki?name=Downloads. The included SynGdiPlus.pas is dated 17/9/11. As it doesn't run 'stand alone', I downloaded SynPdf.zip to get SQLPages.zip, etc., dated 25/9/11. I notice that mORMOT.7z has newer versions via the same downloads page. I've updated and am now bowled over at being able to convince Photoshop that it really is a 300dpi Tif!

BTW. What are the minimum code requirements for SynGdiPlus? I may well need mORMOT one day, but for now I need SynGdi+. Is including just SQLPages enough?)

The antialiasing, resizing and file format conversion work a treat, by the way. All I need now is LZ compression and  conversion to CYMK, but maybe that's asking too much? Could I, if this is the line, ask for amendments to -

SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF, 80, 0, 300);

Many thanks for your great work.

Offline

#17 2012-09-16 08:35:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,132
Website

Re: Draw image and save as tiff

The version included in mORMot is the latest stable version.

The latest available is from http://synopse.info/fossil

The unit is just a wrapper around GDI+ and I suspect GDI+ does not support LZ compression nor CYMK.
So we won't be able to help you much here, sorry...
sad

Offline

#18 2012-09-16 12:01:46

trakless
Member
Registered: 2012-09-14
Posts: 3

Re: Draw image and save as tiff

Thanks AB,

I ploughed through the mORMot documentation, and found -

'Save the picture into any GIF/PNG/JPG/TIFF format - CompressionQuality is used for gptJPG format saving and is expected to be from 0 to 100; for gptTIF format, use ord(TGDIPPEncoderValue) to define the parameter; by default, will use ord(evCompressionLZW) to save the TIFF picture with LZW - for gptTIF, only valid values are ord(evCompressionLZW), ord(evCompressionCCITT3), ord(evCompressionCCITT4), ord(evCompressionRle) and ord(evCompressionNone) function ToBitmap: TBitmap;'

So I tried -

SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF, ord(evCompressionLZW), 0, 300);

which now works and so does ord(evCompressionNone) and would presumably with the other compression settings.

I also noticed the line - 'Sub class to handle .TIF file extension - GDI + seems not able to load all Tiff file formats' which maybe a warning, but there is also -
GDI+ available filling modes TGDIPPEncoderValue = ( evColorTypeCMYK, evColorTypeYCCK, evCompressionLZW, evCompressionCCITT3, evCompressionCCITT4, evCompressionRle, evCompressionNone, evScanMethodInterlaced, evScanMethodNonInterlaced, evVersionGif87, evVersionGif89, evRenderProgressive, evRenderNonProgressive, evTransformRotate90, evTransformRotate180, evTransformRotate270, evTransformFlipHorizontal, evTransformFlipVertical, evMultiFrame, evLastFrame, evFlush, evFrameDimensionTime, evFrameDimensionResolution, evFrameDimensionPage );

I don't understand how I could try this. What would be the Delphi code be that implemented Color, Compression and Dpi? It appears only three parameters are allowed. I tried -

SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF, ord(evCompressionLZW), ord(evColorTypeCMYK), 300);
and then
SynGDIPlus.SaveAs(Bmp, 'c:\test.tif', gptTIF, ord(evColorTypeCMYK), 0, 300);

both of which ran OK, but don't do anything to the colour, which remains stubbonly at RGB. Interestingly, there's no evColorTypeRGB mentioned.

Any help appreciated.

Offline

Board footer

Powered by FluxBB