Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't use <code>TPicture.LoadFromFile</code> if you don't know what format the graphic has, as this method uses the file extension to determine which of the registered graphic formats needs to be loaded. There's a reason that there is no matching <code>TPicture.LoadFromStream</code> method.</p> <p>An external library which can examine data and determine the graphic format at runtime would be the best solution. You could use the <a href="http://www.efg2.com/Lab/Library/Delphi/Graphics/FileFormatsAndConversion.htm" rel="noreferrer">efg page</a> as a starting point of your research.</p> <p>A quick and dirty solution is to try the few formats you need to handle until one succeeds:</p> <pre><code>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 &lt;&gt; 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; </code></pre> <p><strong>Edit:</strong></p> <p>The <a href="http://www.soft-gems.net/index.php?option=com_content&amp;task=view&amp;id=13&amp;Itemid=33" rel="noreferrer">GraphicEx library</a> has an example <em>convert</em> that uses</p> <pre><code>GraphicClass := FileFormatList.GraphicFromContent(...); </code></pre> <p>to determine the graphic format. This seems very similar to the VB6 way of doing this that you mention. Maybe you can use this library for your purpose.</p>
 

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