function LoadPicture(
const AFile:
string;
var pPicture: IPicture):Boolean;
const
IID_IPicture : TGUID = '
{7BF80980-BF32-101A-8BBB-00AA00300CAB}';
var
hFile, hMem: THandle;
dwFileSize, dwBytesRead: DWord;
pData: Pointer;
bRead: Boolean;
hRes: HResult;
pStream: IStream;
begin
Result := False;
bRead := False;
dwBytesRead := 0;
//Datei öffnen
hFile := CreateFile(PChar(AFile), GENERIC_READ, 0,
NIL, OPEN_EXISTING, 0, 0);
if hFile <> INVALID_HANDLE_VALUE
then
begin
try
//Dateigröße ermitteln
dwFileSize := GetFileSize(hFile,
nil);
if dwFileSize <> INVALID_FILE_SIZE
then
begin
//GlobalMemory reservieren und gleichzeitig mit "Nullen" füllen
hMem := GlobalAlloc(GMEM_MOVEABLE
{ or GMEM_NODISCARD} or GMEM_ZEROINIT, dwFileSize);
if hMem <> 0
then
begin
//Adresse des ersten Bytes des Speicher-Objects abfragen
pData := GlobalLock(hMem);
if pData <>
nil then
begin
try
//Daten in das Speicher-Object lesen
bRead := ReadFile(hFile, pData^, dwFileSize, dwBytesRead,
nil);
finally
//Ich bin mit schreiben fertig, Sperre weg --> Daten bleiben
GlobalUnlock(hMem);
end;
end;
if (bRead = True)
and (dwFileSize = dwBytesRead)
then
begin
//Aus GobalMemory IStream erzeugen --> freigeben??
//(** wird der Stream freigegeben wird auch automatisch
//GlobalMemory freigegeben)
pStream :=
nil;
hRes := CreateStreamOnHGlobal(hMem, True
{**}, pStream);
if (hRes = S_OK)
and (pStream <>
nil)
then
begin
//IPicture aus der Bilddatei in IStream erzeugen --> freigeben??
hRes := OleLoadPicture(pStream, dwFileSize, False, IID_IPicture, pPicture);
if (hRes = S_OK)
and (pPicture <>
nil)
then
Result := True;
end;
end;
end;
end;
finally
//Datei wieder zumachen
CloseHandle(hFile);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
BildDateiname = '
C:\darc.jpg';
var
DC: HDC;
hmHeight, hmWidth, nHeight, nWidth: Integer;
rc: TRect;
pPicture: IPicture;
begin
if LoadPicture(BildDateiname, pPicture)
then
begin
DC := GetDC(
Handle);
if(pPicture.get_Width(hmWidth) = S_OK)
and
(pPicture.get_Height(hmHeight) = S_OK)
and
(Windows.GetClientRect(
Handle,rc))
then
begin
nWidth := MulDiv(hmWidth,GetDeviceCaps(
DC,LOGPIXELSX),2540);
nHeight := MulDiv(hmHeight,GetDeviceCaps(
DC,LOGPIXELSY),2540);
pPicture.Render(
DC, 0, 0, nWidth, nHeight, 0, hmHeight,
hmWidth, -hmHeight, rc);
end;
ReleaseDC(
Handle,
DC);
//pPicture und zugrundeliegendes IStream wieder freigeben:
//???
end;
end;