XML ファイルから画像を読み込む必要があります。XML ファイルには、画像が JPG/GIF/BMP であるかどうかに関する情報はありません。画像を読み込んだ後、ビットマップに変換する必要があります。
実際のファイル形式を知らなくても、画像をビットマップに変換する方法を知っている人はいますか? Delphi 2007/2009 を使用しています
ありがとうございました。
XML ファイルから画像を読み込む必要があります。XML ファイルには、画像が JPG/GIF/BMP であるかどうかに関する情報はありません。画像を読み込んだ後、ビットマップに変換する必要があります。
実際のファイル形式を知らなくても、画像をビットマップに変換する方法を知っている人はいますか? Delphi 2007/2009 を使用しています
ありがとうございました。
Delphi 2009 には、JPEG、BMP、GIF、および PNG のサポートが組み込まれています。
以前のバージョンの Delphi では、PNG および GIF のサード パーティの実装を見つける必要がある場合がありますが、Delphi 2009では、uses 句にJpeg,pngimageおよびユニットを追加するだけです。GIFImg
ファイルに拡張子がある場合は、次のコードを使用できます.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;
もっと簡単な方法を見つけました!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;
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の方法と非常によく似ているようです。このライブラリを目的に使用できるかもしれません。