[How to] How to use PNG and GIF images in TRichView
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
[How to] How to use PNG and GIF images in TRichView
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/thir ... gimage.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.
Update: starting from TRichView 14.5, TRichView uses TPngImage automatically. So, for Delphi 2009+, no additional code is required to enable PNG support.
Steps to enable PNG support in Delphi 4-2007
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 RVGraphicHandler.RegisterHTMLGraphicFormat(TPngObject); to save images in PNG files (RVGraphicHandler is defined in RVFuncs unit)
4) Call RVGraphicHandler.RegisterPngGraphic(TPngObject); It allows loading PNG images from RTF files and saving PNG images to RTF files without converting to bitmaps or metafiles
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.)
6) (optional step) enable high-quality resizing of non-transparent PNG images, see http://www.trichview.com/forums/viewtop ... 153#p34153
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.
Update 2013-Jun-7: The information above is updated for compatibility with TRichView 14.5:
- now, TRichView uses TPngImage automatically in Delphi 2009+ (unless you define RVDONOTUSEPNGIMAGE in RV_Defs.inc)
- RVGraphicHandler should be used instead of the old functions
Update 2017-Nov-21: the step 6 is added
- 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/thir ... gimage.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.
Update: starting from TRichView 14.5, TRichView uses TPngImage automatically. So, for Delphi 2009+, no additional code is required to enable PNG support.
Steps to enable PNG support in Delphi 4-2007
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 RVGraphicHandler.RegisterHTMLGraphicFormat(TPngObject); to save images in PNG files (RVGraphicHandler is defined in RVFuncs unit)
4) Call RVGraphicHandler.RegisterPngGraphic(TPngObject); It allows loading PNG images from RTF files and saving PNG images to RTF files without converting to bitmaps or metafiles
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.)
6) (optional step) enable high-quality resizing of non-transparent PNG images, see http://www.trichview.com/forums/viewtop ... 153#p34153
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.
Update 2013-Jun-7: The information above is updated for compatibility with TRichView 14.5:
- now, TRichView uses TPngImage automatically in Delphi 2009+ (unless you define RVDONOTUSEPNGIMAGE in RV_Defs.inc)
- RVGraphicHandler should be used instead of the old functions
Update 2017-Nov-21: the step 6 is added
Last edited by Sergey Tkachenko on Fri Jun 07, 2013 2:50 pm, edited 5 times in total.
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
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/thir ... fimage.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).
- 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/thir ... fimage.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.
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
-
- Posts: 131
- Joined: Mon Aug 29, 2005 12:03 pm
Typo in documentation
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
Unit RVMisc.
procedure RV_RegisterPngGraphic(ClassType: TGraphicClass);
whereas the correct unit is CRVData, as you mentioned above.
Cheers,
Martin
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
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:.
Because of this, even if you add the line 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:
All other registration code can still be for 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: Select all
TPNGObject = TPngImage
Because of this, even if you add the line
Code: Select all
RegisterClass(TPngObject)
Solution
Create your own class with the name TPngObject and register it:
Code: Select all
type TPngObject = class (TPngImage);
...
RegisterClass(TPngObject);
Code: Select all
RegisterClass(TPngImage);
RV_RegisterHTMLGraphicFormat(TPngImage);
RV_RegisterPngGraphic(TPngImage);
Another possibility is using the method Classes.RegisterClassAlias to register TPNGObject as an alias for TPngImage.
Delphi code:
C++ code:
Delphi code:
Code: Select all
RegisterClassAlias(TPngImage, 'TPNGObject');
Code: Select all
RegisterClassAlias(__classid(TPngImage), "TPNGObject");
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
-
- Posts: 130
- Joined: Fri Mar 04, 2011 3:44 am
Does this still apply to Delphi XE and TRichView 13.6?
Noticed that the usage of PNGs isn't working at the moment. Searched the forum and found this thread, but it seems not to work to call
Noticed that the usage of PNGs isn't working at the moment. Searched the forum and found this thread, but it seems not to work to call
Code: Select all
RegisterClass(TPngImage); <- incompatible types
RV_RegisterHTMLGraphicFormat(TPngImage); <- unknown, although CRVFData is in uses
RV_RegisterPngGraphic(TPngImage); <- unknown, although CRVFData is in uses
TPicture.RegisterFileFormat('PNG', 'Portable Network Graphics', TPngImage);
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
-
- Posts: 130
- Joined: Fri Mar 04, 2011 3:44 am
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
-
- Posts: 130
- Joined: Fri Mar 04, 2011 3:44 am
translated:
for UnRegisterClass(TPngImage); in the finalization-part:
Code: Select all
[DCC Fehler] <my_unit>: E2010 incompatible types: 'tagWNDCLASSW' and 'class of TPngImage'
Code: Select all
[DCC Fehler] <my_unit>: E2010 incompatible types: 'PWideChar' and 'class of TPngImage'
-
- Site Admin
- Posts: 17522
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
-
- Posts: 130
- Joined: Fri Mar 04, 2011 3:44 am