19

我必须从 XML 文件加载图像。XML 文件中没有关于图像是否为 JPG/GIF/BMP 的信息。加载图像后,我需要将其转换为位图。

有没有人知道如何在不知道实际文件格式的情况下将图像转换为位图?我正在使用德尔福 2007/2009

谢谢你。

4

4 回答 4

40

Delphi 2009 内置了对 JPEG、BMP、GIF 和 PNG 的支持。

对于早期版本的 Delphi,您可能需要找到 PNG 和 GIF 的第三方实现,但在 Delphi 2009 中,您只需将Jpeg,pngimageGIFImg单位添加到您的 uses 子句。

如果文件有扩展名,您可以使用以下代码,正如其他人所指出的那样,TPicture.LoadFromFile 会查看继承类注册的扩展名以确定要加载的图像。

uses
  Graphics, Jpeg, pngimage, GIFImg;

procedure TForm1.Button1Click(Sender: TObject);
var
  Picture: TPicture;
  Bitmap: TBitmap;
begin
  Picture := TPicture.Create;
  try
    Picture.LoadFromFile('C:\imagedata.dat');
    Bitmap := TBitmap.Create;
    try
      Bitmap.Width := Picture.Width;
      Bitmap.Height := Picture.Height;
      Bitmap.Canvas.Draw(0, 0, Picture.Graphic);
      Bitmap.SaveToFile('C:\test.bmp');
    finally
      Bitmap.Free;
    end;
  finally
    Picture.Free;
  end;
end;

如果文件扩展名未知,一种方法是查看前几个字节以确定图像类型。

procedure DetectImage(const InputFileName: string; BM: TBitmap);
var
  FS: TFileStream;
  FirstBytes: AnsiString;
  Graphic: TGraphic;
begin
  Graphic := nil;
  FS := TFileStream.Create(InputFileName, fmOpenRead);
  try
    SetLength(FirstBytes, 8);
    FS.Read(FirstBytes[1], 8);
    if Copy(FirstBytes, 1, 2) = 'BM' then
    begin
      Graphic := TBitmap.Create;
    end else
    if FirstBytes = #137'PNG'#13#10#26#10 then
    begin
      Graphic := TPngImage.Create;
    end else
    if Copy(FirstBytes, 1, 3) =  'GIF' then
    begin
      Graphic := TGIFImage.Create;
    end else
    if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
    begin
      Graphic := TJPEGImage.Create;
    end;
    if Assigned(Graphic) then
    begin
      try
        FS.Seek(0, soFromBeginning);
        Graphic.LoadFromStream(FS);
        BM.Assign(Graphic);
      except
      end;
      Graphic.Free;
    end;
  finally
    FS.Free;
  end;
end;
于 2009-06-06T07:36:54.540 回答
13

我找到了一个更简单的方法!它自动加载 JPG/GIF/BMP 等文件,甚至不知道/检查文件格式,并相应地进行转换。它非常适合我。

在这里分享:)

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;
于 2009-06-07T08:57:10.667 回答
9

如果您不知道图形具有什么格式,则不能使用TPicture.LoadFromFile,因为此方法使用文件扩展名来确定需要加载哪些已注册的图形格式。没有匹配TPicture.LoadFromStream方法是有原因的。

可以在运行时检查数据并确定图形格式的外部库将是最佳解决方案。您可以使用efg 页面作为研究的起点。

一个快速而肮脏的解决方案是尝试几种您需要处理的格式,直到成功:

function TryLoadPicture(const AFileName: string; APicture: TPicture): boolean;
const
  GraphicClasses: array[0..3] of TGraphicClass = (
    TBitmap, TJPEGImage, TGIFImage, TPngImage);
var
  FileStr, MemStr: TStream;
  ClassIndex: integer;
  Graphic: TGraphic;
begin
  Assert(APicture <> nil);
  FileStr := TFileStream.Create('D:\Temp\img.dat', fmOpenRead);
  try
    MemStr := TMemoryStream.Create;
    try
      MemStr.CopyFrom(FileStr, FileStr.Size);
      // try various
      for ClassIndex := Low(GraphicClasses) to High(GraphicClasses) do begin
        Graphic := GraphicClasses[ClassIndex].Create;
        try
          try
            MemStr.Seek(0, soFromBeginning);
            Graphic.LoadFromStream(MemStr);
            APicture.Assign(Graphic);
            Result := TRUE;
            exit;
          except
          end;
        finally
          Graphic.Free;
        end;
      end;
    finally
      MemStr.Free;
    end;
  finally
    FileStr.Free;
  end;
  Result := FALSE;
end;

编辑:

GraphicEx 库有一个示例转换,它使用

GraphicClass := FileFormatList.GraphicFromContent(...);

确定图形格式。这似乎与您提到的 VB6 执行此操作的方式非常相似。也许您可以将此库用于您的目的。

于 2009-06-06T08:23:01.170 回答
7

我没有 Delphi 2007 或 2009 来查看这是否适用于其中任何一个版本。但是,在 XE2 中,还有另一个类 in Vcl.Graphicscalled用于处理Microsoft Imaging ComponentTWICImage支持的图像,包括BMP、GIF、ICO、JPEG、PNG、TIF 和 Windows Media Photo。它能够从流中检测图像类型。假设您在名为Image1的表单上有一个:TImage

procedure LoadImageFromStream(Stream: TStream; Image: TImage);
var
  wic: TWICImage;
begin
  Stream.Position := 0;
  wic := TWICImage.Create;
  try
    wic.LoadFromStream(Stream);
    Image.Picture.Assign(wic);
  finally
    wic.Free;
  end;
end;

procedure RenderImage(const Filename: string);
var
  fs: TFileStream;
begin
  fs := TFileStream.Create(Filename, fmOpenRead);
  try
    LoadImageFromStream(fs, Image1);
  finally
    fs.Free;
  end;
end;

它无需在您的语句中添加PNGImage,GIFImg或即可工作。JPEGuses

其他答案显示了如何将 转换TImage为 BMP,所以我不包括在这里。我只是展示了另一种在TImage事先不知道图像类型或文件扩展名的情况下将各种图形类型加载到 a 中的方法......

于 2013-11-19T23:33:42.770 回答