Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Aus DIB ein Bitmap erzeugen (https://www.delphipraxis.net/63176-aus-dib-ein-bitmap-erzeugen.html)

minimops 14. Feb 2006 22:38


Aus DIB ein Bitmap erzeugen
 
Liste der Anhänge anzeigen (Anzahl: 1)
Ich möchte Informationen aus dem Internet Explorer in mein Programm via Drag and Drop ziehen. Text + HTML klappen prima, nur mit den Bildern komme ich nicht zurecht.

Es wird immer nur ein weisses Rechteck angezeigt...

Delphi-Quellcode:
procedure TForm1.HandleDIB(  dataObj : IDataObject; fetc : TFormatEtc );
var
   p   : pointer;
   stgm : TSTGMEDIUM;
   BMIH : TBITMAPINFO;
   bmp  :HBITMAP;
    _HDC, hDC : windows.HDC;
begin
      {Make certain the data rendering is available}
   if(  dataObj.QueryGetData(  fetc ) = NOERROR ) then
   begin
         {Get the data}
      dataObj.GetData(  fetc, stgm );

         {Lock the global memory handle to get
           a pointer to the data}

      BMIH := TBITMAPINFO(GlobalLock(stgm.hGlobal)^);

      with bmih.bmiHeader do bmp:=CreateBitmap (biWidth,biHeight,biPlanes,biBitCount,@bmih.bmiColors);


        _HDC := GetDC(Self.Handle);
         hDC := CreateCompatibleDC(_HDC);
        SelectObject(hDC, bmp);

        BitBlt (hDC, 0, 0, bmih.bmiHeader.biWidth, bmih.bmiHeader.biHeight, img.Canvas.Handle, 0, 0, cmSrcCopy); // Nur zum Füllen von bmp
        BitBlt(_HDC, 0, 0, bmih.bmiHeader.biWidth, bmih.bmiHeader.biHeight, hDC, 0, 0, cmSrcCopy);

         {Finished with the pointer}
      GlobalFree(  stgm.hGlobal );

         {Free the memory}
      ReleaseStgMedium(  stgm );
   end;
end;
Was mach ich falsch????

Danke Axel

Garfield 15. Feb 2006 11:21

Re: Aus DIB ein Bitmap erzeugen
 
Delphi-Quellcode:
      with bmih.bmiHeader do bmp:=CreateBitmap (biWidth,biHeight,biPlanes,biBitCount,@bmih.bmiColors);
Eine Lösung kann ich Dir leider nicht präsentieren, nur eine Vermutung. Der Wert für bmp ist 0, also wurde kein Bitmap erstellt. Die Werte für bmiColor sind auch 0,0,0,0. Kann sein, dass der Farbraum nicht stimmt!?

Flocke 15. Feb 2006 14:12

Re: Aus DIB ein Bitmap erzeugen
 
Du kopierst bei der Zuweisung nur den TBitmapHeader und nicht die Bits, du musst mit Zeigern arbeiten!

Passe die markierten Zeilen an:
Delphi-Quellcode:
procedure TForm1.HandleDIB(  dataObj : IDataObject; fetc : TFormatEtc );
var
   p   : pointer;
   stgm : TSTGMEDIUM;
   BMIH : PBITMAPINFO; // <-- HIER
   bmp  :HBITMAP;
   _HDC, hDC : windows.HDC;
begin
      {Make certain the data rendering is available}
   if(  dataObj.QueryGetData(  fetc ) = NOERROR ) then
   begin
         {Get the data}
      dataObj.GetData(  fetc, stgm );

         {Lock the global memory handle to get
           a pointer to the data}

      BMIH := PBITMAPINFO(GlobalLock(stgm.hGlobal)); // <-- HIER

      with bmih.bmiHeader do bmp:=CreateBitmap (biWidth,biHeight,biPlanes,biBitCount,@bmih.bmiColors);


        _HDC := GetDC(Self.Handle);
         hDC := CreateCompatibleDC(_HDC);
        SelectObject(hDC, bmp);

        BitBlt (hDC, 0, 0, bmih.bmiHeader.biWidth, bmih.bmiHeader.biHeight, img.Canvas.Handle, 0, 0, cmSrcCopy); // Nur zum Füllen von bmp
        BitBlt(_HDC, 0, 0, bmih.bmiHeader.biWidth, bmih.bmiHeader.biHeight, hDC, 0, 0, cmSrcCopy);

         {Finished with the pointer}
      GlobalFree(  stgm.hGlobal );

         {Free the memory}
      ReleaseStgMedium(  stgm );
   end;
end;
Nachträge:
1. Du solltest noch ein paar Resourcenschutzblöcke reinpacken (try...finally).
2. Da du schon eine DIB hast kannst du die auch mit CreateDIBitmap erzeugen.
3. Beim ersten BitBlt überschreibst du BMP mit dem Inhalt aus dem Image-Canvas.
4. Du selektierst BMP nicht wieder aus dem DC heraus.
5. Du gibst BMP nirgendwo wieder frei (DeleteObject).
6. Du gibst hDC nirgendwo wieder frei (DeleteDC).
7. Du gibst _HDC nirgendwo wieder frei (ReleaseDC).


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:44 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz