Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Delphi 2009 comes with built in support for JPEG, BMP, GIF and PNG.</p> <p>For earlier versions of Delphi you may need to find third party implementations for PNG and GIF, but in Delphi 2009 you simply add the <code>Jpeg</code>, <code>pngimage</code> and <code>GIFImg</code> units to your uses clause.</p> <p>If the file has an extension you can use the following code, as noted by others the TPicture.LoadFromFile looks at the extensions registered by the inherited classes to determine which image to load.</p> <pre><code>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; </code></pre> <p>If the file extension is not known one method is to look at the first few bytes to determine the image type.</p> <pre><code>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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload