Link to home
Start Free TrialLog in
Avatar of panJames
panJames

asked on

Passing a bitmap to DLL using handle to it

Hello experts :-)

I need to pass a bitmap to a dll, edit it there and then send to back.

I know (thank you ziolko :-) ) how to do it using (Buffer : Pointer; StreamLength : Int64).

However, I was told that it is possible to pass a handle to bitmap to DLL and make it this way.

Question: how can I do it?

Polon
Avatar of MerijnB
MerijnB
Flag of Netherlands image

do you write the dll yourself?
SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I modified the contents of the button click event to draw to the second bitmap and show that both update...
procedure TForm1.Button1Click(Sender: TObject);
begin
  Bitmap2.Handle := Bitmap1.Handle;
  with Bitmap2, Canvas do
    begin
      MoveTo(0, 0);
      LineTo(99, 99);
    end;
  PaintBox1.Invalidate;
  PaintBox2.Invalidate;
end;

Open in new window

I'm afraid that's not what you looking for but that's best solution I've come up with

library BMPDLL;

uses
  SysUtils,
  Classes,
  Windows,
  Graphics;

{$R *.res}

procedure MakeColorAnalysis3(var Bitmap: HBITMAP);stdcall;
var bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  try
    bmp.Handle := Bitmap;
    with TBitmap.Create do begin
      Assign(bmp);
      Canvas.MoveTo(0, 0);
      Canvas.LineTo(256, 256);
      Bitmap := Handle;
    end;
  finally
    bmp.Free;
  end;  
end;

exports
  MakeColorAnalysis3;

begin
end.

EXE:
interface
procedure MakeColorAnalysis3(var Bitmap: HBITMAP);stdcall;
implementation
procedure MakeColorAnalysis3;external 'bmpdll.dll' name 'MakeColorAnalysis3';

procedure TForm1.Button6Click(Sender: TObject);
var h: HBITMAP;
begin
  h := Image1.Picture.Bitmap.Handle;
  MakeColorAnalysis3(h);
  Image1.Picture.Bitmap.Handle := h;
end;

ziolko.
I've also found some C++ code that supposedly lets you access raw bmp bits represented by HBITMAP (so there's no need to create additional bmp in DLL and re-assign Handle on EXE side) but can't make it work all I got is scrambled image.

ziolko.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
 with TmpBMP do begin
      ReleaseHandle;    
      Assign(bmp);
      Canvas.MoveTo(0, 0);

ziolko.
any progress on this one?


ziolko.