Re: Exchanging bitmaps with DLL?
From: Matthias Matting (matting_at_matting.de)
Date: 10/11/03
- Next message: Paul Surgeon: "Re: So Delphi can't do this ..."
- Previous message: Jens Gruschel: "Re: Calculating a date in the future"
- In reply to: Peter Below (TeamB): "Re: Exchanging bitmaps with DLL?"
- Next in thread: Peter Below (TeamB): "Re: Exchanging bitmaps with DLL?"
- Reply: Peter Below (TeamB): "Re: Exchanging bitmaps with DLL?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Paul Surgeon: "Re: So Delphi can't do this ..."
- Previous message: Jens Gruschel: "Re: Calculating a date in the future"
- In reply to: Peter Below (TeamB): "Re: Exchanging bitmaps with DLL?"
- Next in thread: Peter Below (TeamB): "Re: Exchanging bitmaps with DLL?"
- Reply: Peter Below (TeamB): "Re: Exchanging bitmaps with DLL?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|