Application of GDI + in Delphi-mutual conversion between GDI + image and GDI bitmap

Source: Internet
Author: User

Delphi's tbitmap encapsulates Windows's GDI bitmap. Therefore, tbitmap only supports BMP format images. However, in Delphi applications, conversion of the image format is often encountered, for example, convert the Delphi bitmap tbitmap image to another format for storage, or convert other image formats to tbitmap. At this time, we often use some third-party components or code. The tjpeg. Pas that comes with Delphi is the third-party code unit for JPEG format image conversion.

In fact, using tgpbitmap of GDI + can easily implement mutual conversion between some image formats with tbitmap, the following code converts a PNG image to a timage image on the Delphi window interface for display:

VaR
BMP: tgpbitmap;
Begin
// BMP: = tgpbitmap. Create ('d: del_gdiplusdemosmediamsn1.gif ');
// BMP: = tgpbitmap. Create ('d: del_gdiplusdemosmediamultiframe.tif ');
BMP: = tgpbitmap. Create ('d: del_gdiplusdemosmediaclimber.png ');
Image1.picture. bitmap. Handle: = BMP. gethbitmap (0 );
BMP. Free;
End;

In the code, we first load the PNG format image from the disk file with the tgpbitmap of GDI +, and then directly take the image handle of the GDI + bitmap and assign it to the handle attribute of tbitmap to complete the conversion. Tgpbitmap in the code. the gethbitmap method has a background color parameter during image conversion. For non-transparent images, this parameter is ignored. Because the tbitmap method of Delphi does not support transparent images, this parameter does not work even if a PNG Image has a transparent part. No matter what color you set, the transparent background is always black. As long as the image formats supported by GDI + are supported, they can be easily converted to tbitmap. The annotated codes in the Code are converted to gif and TIFF image formats respectively.

Similarly, we can use GDI + to convert the tbitmap image to the supported format:

VaR
BMP: tgpbitmap;
G: tgpgraphics;
CLSID: tguid;
Begin
// Use the image handle of timage. Picture. bitmap and the palette attribute palette to create a GDI + bitmap
With image1.picture. Bitmap do
BMP: = tgpbitmap. Create (handle, palette );
// Convert to PNG format for saving
If getencoderclsid ('image/PNG ', CLSID) then
BMP. Save ('d: gdi_test.png ', CLSID );
// Display in the window
G: = tgpgraphics. Create (handle, false );
G. drawimage (BMP, 0, 0 );
BMP. Free;
G. Free;
End;

In the preceding example, the image of the timage control is converted to a PNG image. Note that if a third-party code is converted to a mounted timage image, the preceding conversion may not be supported, for example, the image loaded using tsf-image, because tjpegimage is inherited from tgraphic and does not provide the handle attribute of hbitmap type, the conversion will not succeed.

You can also use GDI + to compress and save the tbitmap bitmap. The following example compresses the timage image by 50% and saves it as a JPEG image:

VaR
BMP: tgpbitmap;
G: tgpgraphics;
CLSID: tguid;
Parameters: tencoderparameters;
Quality: integer;
Guid: tguid;
Begin
With image1.picture. Bitmap do
BMP: = tgpbitmap. Create (handle, palette );
If getencoderclsid ('image/JPEG ', CLSID) then
Begin
Parameters. Count: = 1;
Parameters. Parameter [0]. guid: = encoderquality;
Parameters. Parameter [0]. valuetype: = encoderparametervaluetypelong;
Parameters. Parameter [0]. numberofvalues: = 1;
Quality: = 50; // picture quality 50
Parameters. Parameter [0]. Value: = @ quality;
BMP. Save ('d: gdi_test.jpg ', CLSID, @ parameters );
End;
G: = tgpgraphics. Create (handle, false );
G. drawimage (BMP, 0, 0 );
BMP. Free;
G. Free;
End;

The current version of GDI + only supports JPEG image format compression. For details about how to set the image encoder Compression Parameters in the example, see my article "Application of GDI + in Delphi-multiple frames (pages) decomposition and synthesis of images.

In the Delphi program, you can use the topenpicturedialog and tsavepicturedialog dialogs to access images. Likewise, there are no third-party code support and there are only a few image formats to choose from, we can use GDI + to write a simple conversion code unit, and add this unit to the uses part of the program code unit to implement more image format options.

Unit gdipbitmap;

Interface

Uses
Gdiptypes, gdiplus, windows, sysutils, classes, graphics;

Type
Tgdipbitmap = Class (tbitmap)
Public
Procedure loadfromstream (Stream: tstream); override;
Procedure savetofile (const filename: string); override;
Procedure savetostream (Stream: tstream); override;
End;

Implementation

{Tgdipbitmap}

VaR
Imageformat: String = '';

Procedure setimageformat (const filename: string );
Begin
Imageformat: = extractfileext (filename );
If imageformat <> ''then
Begin
Delete (imageformat, 1, 1 );
If comparetext (imageformat, 'jpg ') = 0 then
Imageformat: = 'jpeg'
Else if comparetext (imageformat, 'tif ') = 0 then
Imageformat: = 'tiff ';
End else imageformat: = 'bmp ';
Imageformat: = 'image/'+ imageformat;
End;

Procedure tgdipbitmap. loadfromstream (Stream: tstream );
VaR
Adaper: tstreamadapter;
TMP: tgpbitmap;
Begin
Adaper: = tstreamadapter. Create (stream, soreference );
TMP: = tgpbitmap. Create (adaper );
Try
Handle: = TMP. gethbitmap (0 );
Finally
TMP. Free;
End;
End;

Procedure tgdipbitmap. savetofile (const filename: string );
Begin
Setimageformat (filename );
Inherited savetofile (filename );
Imageformat: = '';
End;

Procedure tgdipbitmap. savetostream (Stream: tstream );
VaR
TMP: tgpbitmap;
Adaper: tstreamadapter;
CLSID: tguid;
Begin
If (imageformat <> '') and (getencoderclsid (imageformat, CLSID) then
Begin
TMP: = tgpbitmap. Create (handle, palette );
Try
Adaper: = tstreamadapter. Create (stream, soreference );
TMP. Save (adaper, CLSID );
Finally
TMP. Free;
End;
End else
Inherited savetostream (Stream );
End;

Initialization
// Tpicture. registerfileformat ('bmp ', 'bmp file', tgdipbitmap );
Tpicture. registerfileformat ('exif', 'tiff file', tgdipbitmap );
Tpicture. registerfileformat ('tiff ', 'tiff file', tgdipbitmap );
Tpicture. registerfileformat ('tif ', 'tiff file', tgdipbitmap );
Tpicture. registerfileformat ('png ', 'png file', tgdipbitmap );
Tpicture. registerfileformat ('gif', 'gif file', tgdipbitmap );
Tpicture. registerfileformat ('jpeg ', 'jpeg file', tgdipbitmap );
Tpicture. registerfileformat ('jpg ', 'jpg file', tgdipbitmap );
Finalization
Tpicture. unregistergraphicclass (tgdipbitmap );
End.

The above is a simple GDI + image conversion unit I wrote. In the initialization part of the unit, I registered an access class tgdipbitmap for several image formats with Delphi. The BMP format registration code was canceled, it is better to use the default tbitmap. The conversion principle mentioned above is used in the Code. However, the tbitmap image format converted in this way is 32-bit and can be used in tgdipbitmap. handle: = TMP. gethbitmap (0); Add code after the statement to convert the image pixel format:

Handle: = TMP. gethbitmap (0 );
Case TMP. pixelformat
Pf1bppindexed: pixelformat: = pf1bit;
Pf4bppindexed: pixelformat: = pf4bit;
Pf8bppindexed: pixelformat: = pf8bit;
Pf16bpprgb565, pf16bpprgb555, pf16bppargb1555: pixelformat: = pf16bit;
Pf24bpprgb: pixelformat: = pf24bit;
Pf32bpprgb, pf32bppargb: pixelformat: = pf32bit;
Else pixelformat: = pfcustom;
End;

The following code demonstrates how to use this unit to open an image in the dialog box. It should be noted that when the PNG image format is selected in the IDE debugging and running status of Delphi, the CPU debugging window is displayed. This is gdiplus. the DLL bug is still a Delphi problem, but it does not affect the proper running of the program. It is normal without the IDE environment:

Uses gdiplus, gdipbitmap;
.....
......
Procedure tform1.button5click (Sender: tobject );
VaR
BMP: tgpbitmap;
G: tgpgraphics;
Begin
If openpicturedialog1.execute then
Begin
BMP: = tgpbitmap. Create (openpicturedialog1.filename );
G: = tgpgraphics. Create (handle, false );
G. drawimage (BMP, 0, 0 );
BMP. Free;
G. Free;
End;
End;

In fact, if you want to, you can use the component-> install Component menu of Delphi to create a new package or add the unit to the default package. After confirming the installation, you can use the picture attribute of timage to select multiple image format files during the design period.

The GDI + unit used in the code in the above example is modified by myself. If the GDI + unit of other versions is used, modify it as appropriate, my GDI + unit can be found in the article "GDI + for VCL basics-GDI + and VCL", and pay attention to the last several modifications in the article.

By the way, even if you do not use a friend of Delphi, you can also use the conversion method in the article to convert the GDI + image and the GDI Bitmap Using hbitmap and hpalette.

If there is an error or suggestion, guidance, please leave a message or letter: maozefa@hotmail.com

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.