#1 2010-09-23 14:07:43

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

GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Our freeware SynGdiPlus unit implements TGraphic descendants able to load and save GIF, TIF, PNG and JPG pictures, using the GDI+ library.
Freeware Opensource component, licensed under a MPL/GPL/LGPL tri-license.
Works from Delphi 3 to 2010.

By adding the SynGdiPlus unit to your uses clause, some new TGraphic descendants are registered in your application to load GIF, TIF, PNG and JPG pictures. Use TPicture.LoadFromFile() method with the right extension, and the corresponding decoder will be used.

This unit implements its own GDI+ API functions, in a safe dynamic manner. Even if you don't have GDI+ installed on the system (e.g. with Windows 2000), the executable will launch - but all GIF, TIF, PNG and JPG pictures loading and saving won't work. Please note that the GDI+ library is available on all Windows Operating System since Windows XP (including Vista and Seven).

Since all the decoding and encoding is done by the GDI+ library, this unit adds much less code to your executable than the standard JPEG and PNG units.

These classes handle the saving of the pictures. Most GDI+ implementation just allows you to load pictures. This unit handle all the saving and conversion between formats. You can easily load an TIF, then save it as a JPG file with a specified Compression Quality, via the SaveAs() method.

Some easy one line helper functions are available:

function LoadFrom(const FileName: TFileName): TBitmap;
procedure SaveAs(Graphic: TPersistent; Stream: TStream; Format: TGDIPPictureType; CompressionQuality: integer=80); overload;
procedure SaveAs(Graphic: TPersistent; const FileName: TFileName; Format: TGDIPPictureType; CompressionQuality: integer=80); overload;

The full source code of this unit (intended to work from Delphi 3 to 2010) is freely downloadable from http://synopse.info/files/SynGdiPlus.zip, or as part of Synopse SQLite3 framework.

Offline

#2 2010-09-23 16:47:02

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

I've uploaded an updated version to the http://synopse.info/files/SynGdiPlus.zip archive file.

It was still in version 1.6.... now you've got the latest version, that is 1.9.

Offline

#3 2010-10-26 15:21:30

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

I've uploaded a 1.10 version.
Minor upgrade, in all cases: some code modifications to compile with Delphi 6 compiler.

Feedback is always welcome.

Offline

#4 2011-02-09 19:19:52

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

I've uploaded a 1.12 version, from the main SQLite3 framework package.

Here are the enhancements:
- added code for error handling, via new TGdipStatus enumeration type;
- now GDI+ handles are stored using THandle instead of plain integer type (in order to prepare a 64 bit version of the unit);
- fixed a problem in rendering bitmaps, e.g. as created in SQLite3Pages;
- fixed a problem in rendering underlined text (GDI+ DrawDriverString doesn't handle underlined or stroken out fonts as expected).

Offline

#5 2011-12-17 05:23:53

lucyfir
Member
Registered: 2011-12-17
Posts: 4

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Problem using SynGDI+

You state:  By adding the SynGdiPlus unit to your uses clause, some new TGraphic descendants are registered in your application to load GIF, TIF, PNG and JPG pictures. Use TPicture.LoadFromFile() method with the right extension, and the corresponding decoder will be used.

I used your TestSynGdiPlus app (works fine for .emf files), and dropped a Button on the form.

In the ButtonClick, I Add this:

       img1.Picure.LoadFromFile('c:\test.png');

and I get the following error:

      Unknown picture file extension (.png)

What am I doing wrong?

Offline

#6 2011-12-17 05:38:31

lucyfir
Member
Registered: 2011-12-17
Posts: 4

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

I found a way that works:

procedure TForm1.Button1Click(Sender: TObject);
var
    pic: TPngImage;
begin
    pic := TPngImage.Create;
    pic.LoadFromFile('C:\Users\christian.landman\Pictures\MotaData.png');
    img1.Picture.Bitmap := pic.ToBitmap;
end;

This works just fine - thank you! (Using Delphi 6 Enterprise - All updates loaded)

I'm just a bit disapointed that it doesn't work the way you say it does...

Offline

#7 2011-12-17 07:21:36

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

This is the standard way to use a TPicture.

Offline

#8 2011-12-17 12:41:11

Sir Rufo
Member
Registered: 2011-10-14
Posts: 24

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

hmmm, except of the memory leaks, cause the pic Instance will never be freed

Offline

#9 2011-12-17 14:58:07

lucyfir
Member
Registered: 2011-12-17
Posts: 4

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Re: Memory Leak

Thank you Sir Rufo and Mr. Administrator

Offline

#10 2011-12-17 20:09:32

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Add a pic.Free

Offline

#11 2012-03-10 15:26:22

Rogeriouk
Member
Registered: 2012-03-10
Posts: 3

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

I am trying to use the SynGDIPlus in Delphi7 and Win7 to load a jpg file but I can´t do it work.
First I tried

jpg.LoadFromFile('d:\teste.jpg');
Image.Picture.Bitmap := jpg.Bitmap

After I tried to undefine the NOTSYNPICTUREREGISTER and use

image.Picture.LoadFromFile('d:\teste.jpg')

In both cases, no error given but the image don´t renderize anything on screen.

If I use the original TJPEGImage and use this code, it works

var

jpg.LoadFromFile('d:\teste.jpg');
Image.Picture.Bitmap.Assign(jpg)

Any clues ?

Offline

#12 2012-03-10 21:28:20

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Try using it like that:

      Image.Picture.Graphic := TPngImage.Create;
      TPngImage(Image.Picture.Graphic).LoadFromResourceName(HInstance,'wizard2');

That is, use directly the Image.Picture.Graphic and not the Image.Picture.Bitmap property.
It will also be faster, since it will draw it directly to the canvas, no via a temporary bitmap.

Offline

#13 2012-03-15 21:19:02

Rogeriouk
Member
Registered: 2012-03-10
Posts: 3

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

I have the same result.

No error in the process but the image does not render in the container canvas ( a TPanel ). Any other idea ?

Offline

#14 2012-03-15 23:12:14

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Post some code to reproduce the issue.

It is working in a lot of programs, so we need to know what is wrong with your code, or find out if there is any error in our unit.

Offline

#15 2012-03-16 15:26:24

Rogeriouk
Member
Registered: 2012-03-10
Posts: 3

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

AB, Your code work as intended. I did a new project with only a button and a TImage and it worked as you pointed.


After inspecting my project where I am creating the TImage on runtime I found my error and now I can use what I expected:

   Image.Picture.LoadFromFile('D:\test.jpg')

and it is working !

Thank you for you help

Last edited by Rogeriouk (2012-03-16 21:42:49)

Offline

#16 2012-06-24 03:52:00

Pontiac
Member
Registered: 2012-06-24
Posts: 2

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

I look forward to using these units!

I'm running Win7 Pro 64-bit, compiling under D7Pro 7.2.  I'm trying to use the LoadFromFile routine.  Tracing through your code, I get stopped at line 1200:

  if not Gdip.Exists or not FileExists(FileName) then
    exit;

Tracing through it looks through the function [Exists] returns, but then immediately jumps to the end of the LoadFromFile routine.  Doesn't even hit the EXIT command.

So I tried changing the logic of the code, JUST to make sure.

procedure TSynPicture.LoadFromFile(const FileName: string);
var FS: TFileStream;
begin // don't use direct GDI+ file oriented API: it's better having a local
  // copy of the untouched data in memory (e.g. for further jpeg saving)
  Clear;
  if (Gdip.Exists) then
    if (FileExists(FileName)) then begin
      FS := TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone);
      try
        LoadFromStream(FS);
      finally
        FS.Free;
      end;
    end;

end;

So apparently GDIP actually doesn't exist?  It goes again through the function Exists, but then drops right to the end of the function.

What am I doing wrong?

Here is the code I'm using:

procedure TForm1.Button2Click(Sender: TObject);
var
  Pic:TSynPicture;

const fname='C:\Users\Stephen\Dropbox\Photos\Grand Am\DSC00824.jpg';

begin
  Pic:=TSynPicture.Create;
  Pic.LoadFromFile(fname);
  Image1.Picture.Graphic:=Pic;

  image2.Picture.LoadFromFile(fname);
end;

Image2 does render the image.

Last edited by Pontiac (2012-06-24 03:59:09)

Offline

#17 2012-06-24 16:04:09

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Read the documentation about SynGDIPlus unit, or even just read the interface part of it:

var
  /// GDI+ library instance
  // - only initialized at program startup if the NOTSYNPICTUREREGISTER is NOT
  // defined (which is not the default)
  // - Gdip.Exists return FALSE if the GDI+ library is not available in this
  // operating system (e.g. on Windows 2000) nor the current executable folder
  Gdip: TGDIPlus = nil;

To use pictures in your code, you will need to call once:

  Gdip.RegisterPictures; // will initialize the Gdip library if necessary

Offline

#18 2012-06-25 09:23:13

Pontiac
Member
Registered: 2012-06-24
Posts: 2

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Thanks for the heads up.  I undefined the compiler directive, re-ran, and now I have a successful result.

But I'm having an issue trying to get a JPG that is in a stream into a graphic.  I'm going to play some more before I whine about something else I didn't read about. {smirk}

Edit: Over thought things at this end.  All working as intended.  TYVM.

Last edited by Pontiac (2012-06-25 09:30:14)

Offline

#19 2012-06-25 13:02:09

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Nice!

Thanks for the report and update!
smile

Offline

#20 2012-07-12 07:39:19

TPrami
Member
Registered: 2010-07-06
Posts: 116

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Hello,

Would it be possible to make SynGDIPlus (if not already) dynamic so it programs without GDI+ (Old ones), would not crash... So program could disable some features dynamically or inform user, to try to install GDI+ (or someting similar)...

I have made dynamic version of some Gdi+ Delphi implementation, but seems that it will not compile in D2010+ so, would like to have some implementation that would work for DD7 and D2010, before we can jump away from  the D7.

-Tee-

Offline

#21 2012-07-12 15:17:22

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

This is already the case.

Loading is made dynamically, and it is able to find the best available GdiPlu.dll library (e.g. link to the one supplied with Microsoft Office, if it is newer than the one included within XP).

In fact, the program has to check for GDIPlus availability, and use anti-aliaised drawing only if available.

Offline

#22 2013-11-03 18:09:50

andieyudha
Member
Registered: 2013-11-03
Posts: 1

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Dear synopse,

I need to show the .tif format but
I've a problem when try to open TestSynGDIPlus.dpr
There is an error showed up : "Cannot find resource file "d:/TestSynGDIPlus.res. Recreated."
Please some advices.

Many Thank's.

Offline

#23 2013-11-03 21:23:37

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

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

IMHO this is not an error.
There is no boundled .res file - just ignore the warning.
File has been recreated by the IDE.
Then it will work as expected.

Offline

#24 2016-04-29 12:00:11

fs999
Member
Registered: 2014-06-25
Posts: 7

Re: GIF, TIF, PNG and JPG pictures TGraphic read/write via GDI+

Hello,

Is there a way to support the Zip or deflate compression method with Tiff images ?

Kind regards
Frederic

Last edited by fs999 (2016-04-29 12:04:17)

Offline

Board footer

Powered by FluxBB