Delphi's TBitmap encapsulates the Windows GDI bitmap, therefore, TBitmap only supports bmp image format, but in Delphi applications, often encounter convert graphics formats, such as the Delphi TBitmap the bitmap image is converted to other formats save or other image format converting TBitmap like. At this time, we tend to use some third-party components or code, Delphi comes with the third-party code is TJPEG.pas unit jpeg image format conversion.
In fact, the use of GDI + TGpBitmap TBitmap and can easily achieve some image format conversion, the following code to convert the image to a PNG format image on Delphi TImage display window interface:
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;
Tag is first charged with GDI + TGpBitmap the png format image file from a disk, and direct access to GDI + bitmap image is assigned to handle the TBitmap Handle property, to complete the conversion. TGpBitmap.GetHBITMAP method code in the background color image conversion parameters, the non-transparent image, this parameter is ignored, since the Delphi TBitmap image does not support transparency, so even if there is a transparent portion of the PNG image, the parameter nor what role, no matter what you set the color, transparent background is always black. As long as GDI + supports image formats can be easily converted to TBitmap, the code is commented code are GIF and TIFF image format conversion.
Similarly, we can also use GDI + TBitmap image is converted to a format supported by GDI +:
BMP: TGpBitmap;
G: TGpGraphics;
the Clsid: TGUID;
the begin
// Handle handle and using the image attribute palette to create a Palette TImage.Picture.Bitmap the GDI + bitmap
with Image1.Picture.Bitmap do
BMP: = TGpBitmap.Create (the Handle, the Palette);
// save converted to PNG format
IF GetEncoderClsid ( 'Image / PNG', the Clsid) the then
bmp.Save ( 'D: gdi_test.png', the Clsid);
// in the window
g: = TGpGraphics. the Create (the Handle, False);
g.drawImage (BMP, 0, 0);
bmp.Free;
g.Free;
End;
The above examples convert the image TImage control became png format images, note that if a third-party transcoding loaded TImage image, there may not support the above conversion, such as the use TJPEGImage mounted image, because TJPEGImage is inherited from TGraphic come, do not provide the type of HBITMAP Handle property, so the conversion will not succeed.
GDI + can also be used to compress the bitmap stored TBitmap, TImage The following example is 50% compression image jpeg format images stored:
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; // 图片质量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 GDI + version only supports compressed jpeg image format, an example of the image encoder compresses parameter settings can be found in my article " GDI + application Delphi program - Decomposition and synthesis of multi-frame (page) of the image ", which have a more detailed explanation.
In the Delphi program can be implemented TOpenPictureDialog and TSavePictureDialog dialog box to access images, equally, there is no support for third-party code, and only a limited number of image formats to choose from, we can use GDI + to write a simple conversion unit of code in the program unit uses part of the code added to the cells, can be achieved more image format options.
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.
Above is what I wrote a simple GDI + image conversion unit, the initialization part of the unit, Delphi registered to access several image formats like TGdipBitmap, bmp format in which the registration code is written off, or open with the default as well TBitmap . The code used is the previously mentioned conversion principle, however, in this manner are image format conversion TBitmap 32 may be in the Handle method TGdipBitmap.LoadFromStream: = tmp.GetHBITMAP (0); behind the tagging statement converting image pixel format:
tmp.PixelFormat case of
pf1bppIndexed: Pixel Format: = pf1bit;
pf4bppIndexed: Pixel Format: = pf4bit;
pf8bppIndexed: Pixel Format: = pf8bit;
pf16bppRGB565, pf16bppRGB555, pf16bppARGB1555: Pixel Format: = pf16bit;
pf24bppRGB: Pixel Format: = pf24bit;
pf32bppRGB, pf32bppARGB: Pixel Format: = pf32bit;
else pixel format: = pfCustom;
end;
The following code shows the use of the unit image using the dialog box is opened, it should be reminded that, in the operating state of the IDE debugger Delphi when png image format selected, CPU debug window pops up, which is unknown to Gdiplus.dll BUG or Delphi's problems but does not affect the correct operation of the program, from the IDE environment, everything is normal:
.....
......
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 prefer, you can put the unit through Delphi's Component-> Install Component menu to create a new package or unit is added to the default package, after determining the installation can be directly used TImage's Picture property at design time choose a variety of image file formats.
GDI above example used in the code + unit is my own rewrite, if other versions of GDI + unit, should make appropriate changes, my GDI + units Download can article " GDI + for VCL basis - GDI + and VCL " in find, and pay attention to several changes last article.
Incidentally, even without the use of Delphi's friend, also available in paper conversion method, the use of GDI HBITMAP and HPALETTE, achieve mutual conversion GDI + and GDI bitmap image.