加载 Jpg/Gif/Bitmap 并转换为 Bitmap [英] Load Jpg/Gif/Bitmap and convert to Bitmap
本文介绍了加载 Jpg/Gif/Bitmap 并转换为 Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从 XML 文件加载图像.XML 文件中没有关于图像是否为 JPG/GIF/BMP 的信息.加载图像后,我需要将其转换为位图.
I have to load an image from an XML file. There is no information in the XML file about whether the image is JPG/GIF/BMP. After loading the image, I need to convert it to Bitmap.
有没有人知道如何在不知道实际文件格式的情况下将图像转换为位图?我使用的是 Delphi 2007/2009
Does anyone have any clue how to convert images to Bitmap without knowing the actual file format? I'm using Delphi 2007/2009
谢谢.
推荐答案
我找到了一个更简单的方法!它甚至无需知道/检查文件格式即可自动加载 JPG/GIF/BMP 等文件,并相应地进行转换.它对我非常有效.
I've found a simpler way! It loads JPG/GIF/BMP etc. files automatically without even knowing/checking the file format, and convert that accordingly. It worked for me perfectly.
在这里分享:)
Uses
Classes, ExtCtrls, Graphics, axCtrls;
Procedure TForm1.Button1Click(Sender: TObject);
Var
OleGraphic : TOleGraphic;
fs : TFileStream;
Source : TImage;
BMP : TBitmap;
Begin
Try
OleGraphic := TOleGraphic.Create; {The magic class!}
fs := TFileStream.Create('c: estjpg.dat', fmOpenRead Or fmSharedenyNone);
OleGraphic.LoadFromStream(fs);
Source := Timage.Create(Nil);
Source.Picture.Assign(OleGraphic);
BMP := TBitmap.Create; {Converting to Bitmap}
bmp.Width := Source.Picture.Width;
bmp.Height := source.Picture.Height;
bmp.Canvas.Draw(0, 0, source.Picture.Graphic);
image1.Picture.Bitmap := bmp; {Show the bitmap on form}
Finally
fs.Free;
OleGraphic.Free;
Source.Free;
bmp.Free;
End;
End;
这篇关于加载 Jpg/Gif/Bitmap 并转换为 Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文