加载JPG / GIF /位图,并转换为位图 [英] Load Jpg/Gif/Bitmap and convert to Bitmap

查看:346
本文介绍了加载JPG / GIF /位图,并转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从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.

没有人有任何线索如何图像位图转换不知道实际的文件格式?我使用德尔福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:\testjpg.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 /位图,并转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆