trichview.com Forum Index trichview.com
TRichView support forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[How to] How to use PNG and GIF images in TRichView

 
Post new topic   Reply to topic    trichview.com Forum Index -> Examples, Demos
View previous topic :: View next topic  
Author Message
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Fri Sep 09, 2005 7:19 pm    Post subject: [How to] How to use PNG and GIF images in TRichView Reply with quote

PNG images have the following advantages:
- small size (advantage over bitmaps)
- not limited to 256 colors (advantage over gifs)
- lossless compression (advantage over jpegs)
- semitransparency (advantage over all the formats above)
Disadvantage:
- cannot store animation.

For Delphi 4-2007
To use PNG images in TRichView, you need some thirdparty graphic class representing them. For example, you can use free TPngObject (by Gustavo Huffenbacher Daud) http://pngdelphi.sourceforge.net/
(Version 1.564 can be downloaded here:
http://www.trichview.com/resources/thirdparty/pngimage.zip )
It has almost all required functions. The code below assumes that you use this class, but it can be applied to any other class.
For Delphi 2009 or newer
Delphi 2009 has built-in PNG support: TPngImage class from PngImage unit.
See the post below about migrating from TPngObject to TPngImage.

Steps to enable PNG support
(the code below is for Delphi 4-2007; for Delphi 2009+, use TPngImage instead of TPngObject)

1) Add PngImage in "uses" of one of your units

2) Call RegisterClass(TPngObject). It's necessary to load PNG images from RVF files (even if you do not use RVF files, RVF format is used to copy-paste from the Clipboard and in drag&drop)

3) By default, all images are saved to HTML conveted to Jpegs. PNG is a WWW format, so this conversion is undesirable. Call RV_RegisterHTMLGraphicFormat(TPngObject); to save images in PNG files (this function is defined in CRVData unit)

4) Call RV_RegisterPngGraphic(TPngObject); It allows loading PNG images from RTF files and saving PNG images to RTF files without converting to bitmaps or metafiles (this function is also defined in CRVData unit)

5) Usually you need to call TPicture.RegisterFileFormat to associate the class with its file extension. But it's not needed for TPNGObject, because this work is already done in the file where it's implemented: TPicture.RegisterFileFormat('PNG', 'Portable Network Graphics', TPNGObject);. This association is required for many tasks (creating image files when saving HTML, loading external images when loading RTF or HTML, listing in the file filter for "Insert Picture" in RichViewActions, etc.)

The code in steps 2-5 must be called one time before the first file operation. For example, it can be placed in the initialization section of the main form's unit.

Note 1: Only the step 4 is specific for PNG images. All other steps must be applied for any third-party graphic class.

Note 2: There is one step which is not mentioned here: overriding RV_CreateGraphics function. It's required for Delphi versions prior to 6, and for TRichView versions prior to 1.9.12. If you have newer versions of Delphi or TRichView, do not worry about it.


Last edited by Sergey Tkachenko on Tue Jan 26, 2010 8:55 pm; edited 3 times in total
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Mon Sep 26, 2005 4:03 pm    Post subject: Reply with quote

Features of Gif images:
- small size;
- may be animated;
- may have transparent areas;
- limited to 256 colors (each animation frame may have up to 256 colors, but frames may be in different palettes)

Free Delphi libraries supporting GIF, useful for Delphi 4-2006:

1) Anders Melander's TGifImage

http://www.torry.net/vcl/graphics/gif/gifimage.exe (original)
http://www.trichview.com/resources/thirdparty/gifimage.zip (update - required)
To support animation, include RVGifAnimate unit in your project.
(and set RichView.AnimationMode = rvaniOnFormat).

Update March-27-2008: Anders Melander's web site is reborn. You can find the latest information about TGifImage here: http://melander.dk/delphi/gifimage/

2) TJvGifImage from JEDI's JVCL
http://jvcl.sourceforge.net
To support animation, include RVJvGifAnimate unit in your project.
(and set RichView.AnimationMode = rvaniOnFormat).

Delphi 2007 or newer
Delphi 2007 has TGifImage class in GifImg unit.
To support animation, include RVGifAnimate2007 unit in your project.
(and set RichView.AnimationMode = rvaniOnFormat).


Last edited by Sergey Tkachenko on Tue Jan 26, 2010 8:57 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Sun Apr 15, 2007 12:11 pm    Post subject: Reply with quote

Delphi 2007 supports gif images. It includes TGifImage class (defined in GifImg unit). This is an improved version of Anders Melander's TGifImage.

To support animation using this class, include RVGifAnimate2007.pas in your project.
Back to top
View user's profile Send private message Visit poster's website
martindholmes



Joined: 29 Aug 2005
Posts: 132

PostPosted: Thu Sep 25, 2008 4:19 pm    Post subject: Typo in documentation Reply with quote

The TRichView Help file has this:

Unit RVMisc.

procedure RV_RegisterPngGraphic(ClassType: TGraphicClass);

whereas the correct unit is CRVData, as you mentioned above.


Cheers,
Martin
Back to top
View user's profile Send private message
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Thu Sep 25, 2008 4:26 pm    Post subject: Reply with quote

Thanks Martin, it will be corrected.

Also I'd like to mention that Delphi 2009 includes PNG class (TPngImage) in VCL, so thirdparty class is not required any more.
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Tue Jan 26, 2010 9:03 pm    Post subject: Reply with quote

Migrating from TPngObject to TPngImage

This information is useful if:
- you have RVF documents containing PNG images saved by applications compiled in Delphi 4...2007 (using TPngObject class);
- you upgraded to Delphi 2009 or newer.

Problem
In older versions of Delphi, you used TPngObject class for PNG images, and the name of this this class name was saved RVF files to identify PNG images.
In Delphi 2009/2010, the class for PNG is not TPngObject but TPngImage. TPngObject is declared, but it is declared not as a class, but as an alternative name of TPngImage:
Code:
  TPNGObject = TPngImage
.
Because of this, even if you add the line
Code:
RegisterClass(TPngObject)
it will not help, because this line still registers TPngImage instead of TPngObject. So, when loading RVF file, 'TPngImage' class is still unknown for TRichView.

Solution
Create your own class with the name TPngObject and register it:
Code:
type TPngObject = class (TPngImage);
...
RegisterClass(TPngObject);

All other registration code can still be for TPngImage:
Code:
  RegisterClass(TPngImage);
  RV_RegisterHTMLGraphicFormat(TPngImage);
  RV_RegisterPngGraphic(TPngImage);
Back to top
View user's profile Send private message Visit poster's website
Stefaan



Joined: 16 Feb 2009
Posts: 25

PostPosted: Thu Jan 28, 2010 10:12 am    Post subject: Reply with quote

Another possibility is using the method Classes.RegisterClassAlias to register TPNGObject as an alias for TPngImage.

Delphi code:
Code:
RegisterClassAlias(TPngImage, 'TPNGObject');

C++ code:
Code:
RegisterClassAlias(__classid(TPngImage), "TPNGObject");
Back to top
View user's profile Send private message
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6576

PostPosted: Thu Jan 28, 2010 10:28 am    Post subject: Reply with quote

Thank you. Your solution looks much better, because images will be loaded in TPngImage.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    trichview.com Forum Index -> Examples, Demos All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group