首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

c++builder中怎么实现从png到bmp

2012-04-06 
c++builder中如何实现从png到bmp我安装了pngimage的插件,TPngImage这个类名是不是不对啊? 如何实现这个转

c++builder中如何实现从png到bmp
我安装了pngimage的插件,TPngImage这个类名是不是不对啊? 如何实现这个转化啊?谢谢了

[解决办法]
你用的什么版本的?
[解决办法]
可以看看,希望对你有帮助
Converting from TBitmap to TPNGObject 

TPNGObject provides flexibility by allowing to convert from a Bitmap file format to Portable Network Graphics format. 
This is easily done like in the same way as any graphic class in delphi.
IMPORTANT: Always remember to add pngimage to the unit uses.

Converting from Windows bitmap file to PNG file
This method loads a bitmap and saves it using png format

procedure BitmapFileToPNG(const Source, Dest: String);
var
Bitmap: TBitmap;
PNG: TPNGObject;
begin
Bitmap := TBitmap.Create;
PNG := TPNGObject.Create;
{In case something goes wrong, free booth Bitmap and PNG}
try
Bitmap.LoadFromFile(Source);
PNG.Assign(Bitmap); //Convert data into png
PNG.SaveToFile(Dest);
finally 
Bitmap.Free;
PNG.Free;
end
end;

Converting from PNG file to Windows bitmap file
The above inverse. Loads a png and saves into a bitmap

procedure PNGFileToBitmap(const Source, Dest: String);
var
Bitmap: TBitmap;
PNG: TPNGObject;
begin
PNG := TPNGObject.Create;
Bitmap := TBitmap.Create;
{In case something goes wrong, free booth PNG and Bitmap}
try
PNG.LoadFromFile(Source);
Bitmap.Assign(PNG); //Convert data into bitmap
Bitmap.SaveToFile(Dest);
finally 
PNG.Free;
Bitmap.Free;
end
end;

Converting from TImage to PNG file
This method converts from TImage to PNG. It has full exception handling and allows
converting from file formats other than TBitmap (since they allow assigning to a TBitmap)

procedure TImageToPNG(Source: TImage; const Dest: String);
var
PNG: TPNGObject;
BMP: TBitmap;
begin
PNG := TPNGObject.Create;

{In case something goes wrong, free PNG}
try
//If the TImage contains a TBitmap, just assign from it
if Source.Picture.Graphic is TBitmap then
PNG.Assign(TBitmap(Source.Picture.Graphic)) //Convert bitmap data into png
else begin

//Otherwise try to assign first to a TBimap
BMP := TBitmap.Create;
try
BMP.Assign(Source.Picture.Graphic);
PNG.Assign(BMP);
finally
BMP.Free;
end;

end;

//Save to PNG format
PNG.SaveToFile(Dest);
finally 
PNG.Free;
end
end;

 

 

 


 

热点排行