[How to] How to use PNG and GIF images in TRichView
Posted: Fri Sep 09, 2005 7:19 pm
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