Re: Exchanging bitmaps with DLL?

From: Matthias Matting (matting_at_matting.de)
Date: 10/11/03


Date: Sat, 11 Oct 2003 23:34:17 +0200


> > function ModifyBitmap(BitmapHandleIn: HBitmap; var BitmapHandleOut:
HBitmap):byte; StdCall;
> > var MyBMP: TBitmap;
> > begin
> > Result:=1;
> > MyBMP:=TBitmap.Create;
> > MyBMP.Handle:=BitmapHandle; // creates a copy
> > MyBMP.Canvas.Textout(...);
> > BitmapHandleOut:=MyBMP.Handle;
> > end;

> You have a problem here. If you do not free the MyBmp temp you do have a
> memory leak. If you do free it it will destroy the internal bitmap, so the
> handle you return is no longer valid.

Won't I have the same problem in my GetBitmapFromXxxx-function?

function LoadBitmapFromFile(FileName:PChar; var BitmapHandle: HBitmap):byte;
StdCall;
var iFileName: string;
    ap:TAmicaPicB;
    MyBMP: TBitmap;
begin
     Result:=1;
     iFileName:=string(FileName);
     ap:=TAmicaPicB.Create;
     MyBMP:=TBitmap.Create;
     ap.LoadFromFile(iFileName);
     MyBMP.Assign(ap.Bitmap);
     BitmapHandle:=MyBMP.Handle;
     ap.Free;
end;

In a comment about exchanging bitmaps with DLLs in Google I found the
following:
You also have to be careful in respect of taking ownership of the bitmap. I
explain further. If your DLL that created the original bitmap maintains
ownership, it will obviously have to discard the resources it allocated. In
this case, you would have to call bmp.ReleaseHandle and bmp.ReleasePalette
before calling bmp.Free. If you want the ownership to vest with the program
and not the DLL, you would have to release the resources allocated by your
DLL using bmp.Free (without calling the ReleasHandle and ReleasPalette
methods).
(Source is only in Google Cache
<http://www.google.de/search?q=cache:4141AxgiUh4J:www.experts-exchange.com/P
rogramming/Programming_Languages/Delphi/Q_20741118.html>)

I _thought_ that means if I free the bitmap which is given back to the
program IN the exe with bmp.free, everything is fine. Like:

(Exe)
MyBMP:=TBitmap.Create;
LoadBitmapFromFile(PChar(FileName), BitmapHandle);
MyBMP.Handle:=BitmapHandle;
Image1.Picture.Bitmap.Assign(MyBMP);
MyBMP.Free;

Would MyBMP.ReleaseHandle; MyBMP.ReleasePalette; (called in the DLL)
solve this problem? At least the DLL still works fine (and it won't if I
call MyBMP.Free in the DLL)

> You did not Invalidate the picture.

Oh, I see. Invalidating does help :) I also shortened the ModifyBitmap
function a little, like this:

function ModifyBitmap(var CanvasHandle: hdc):byte; StdCall;
var MyString: string;
begin
     Result:=1;
     MyString:='TestTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT';
     TextOut(CanvasHandle, 0, 50, PChar(MyString), Length(MyString));
end;

Question is, (how) can I access the canvas with Delphi functions if I have
the Hdc?

Thanks again
Matthias



Relevant Pages

  • Old Style Resource "embedding" in VB.NET... please help.
    ... and a provided reference DLL. ... In the SDK are two included samples... ... Public Overridable Sub RegisterPanel(ByVal panelClass As String, ... It's a bitmap with 2 images, so I think the ",0" refers to the ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ERROR_RESOURCE_TYPE_NOT_FOUND error comes
    ... I am unable to load the bitmap image in CListCtrl control. ... Made DLL which contains resources like dialog, ... Made a static lib project which contains classes for resources ...
    (microsoft.public.vc.mfc)
  • Re: Graphic performance
    ... Size of the bitmap you're drawing ... Source of the bitmap (DLL or EXE, it would seem, but where is the EXE or ... DLL located? ... need to change some images on the screen and paint few part of the screen. ...
    (microsoft.public.windowsce.embedded)
  • DLL,MFC,Bitmaps
    ... I have an MFC DLL that reads a file and generates a bitmap which I then need ... This is a CF 1.0 app (so no Graphics.GetDC method ... Plan A: ...
    (microsoft.public.dotnet.framework.compactframework)