I try to send TBitmap to DLL, edit it there and return back.

Hello Experts :-)

I try to do such thing:

- I send TBitmap to DLL
- DLL edits it
- I get it back edited

This is how I tried to achieve it:

procedure TForm1.Button1Click(Sender: TObject);
var LImage : TImage;
      PImage : ^TImage;
begin
  LImage := TImage.Create(Application);
  LImage.Picture.Bitmap := Image1.Picture.Bitmap; //Image1 is a TImage on my form

  PImage := @LImage;

  MakeColorAnalysis(PImage);

  Image1.Picture.Bitmap := LImage.Picture.Bitmap;

  LImage.Free;
end;

xxxxxxxxxxxx
DLL

procedure MakeColorAnalysis(aPImage : Pointer); export;
begin
  TImage(aPImage^).Picture.Bitmap.Canvas.PenPos := Point(0, 0);
  TImage(aPImage^).Picture.Bitmap.Canvas.LineTo(50, 50);
end;

What I expected::
- there should be a line drawn on Image1

What I get:
- Image1 disappears

I also tried to send TBitmap only to DLL but had the same result.
What do I do wrong?

Thank you very much

panJames :-)
panJamesAsked:
Who is Participating?
 
ziolkoConnect With a Mentor Commented:
DLL:
library BMPDLL;

uses
  SysUtils,
  Classes,
  Graphics;

{$R *.res}

procedure MakeColorAnalysis(Buffer: Pointer;var Size: Int64);stdcall;
var ms: TMemoryStream;
    bmp: TBitmap;
begin
  ms := TMemoryStream.Create;
  bmp := TBitmap.Create;
  try
    ms.Write(Buffer^, Size);
    ms.Position := 0;
    bmp.LoadFromStream(ms);
    bmp.Canvas.MoveTo(10, 10);
    bmp.Canvas.LineTo(100, 100);
    ms.Clear;
    bmp.SaveToStream(ms);
    Size := ms.Size;
    ms.Position := 0;
    ms.Read(Buffer^, size);
  finally
    ms.Free;
    bmp.FreeImage;  
  end;
end;

exports
  MakeColorAnalysis;

begin
end.

EXE:
interface
  procedure MakeColorAnalysis(Buffer: Pointer;var Size: Int64);stdcall;
...

implementation
  procedure MakeColorAnalysis;external 'bmpdll.dll' name 'MakeColorAnalysis';

...

procedure TForm1.Button5Click(Sender: TObject);
var ms: TMemoryStream;
    buff: Pointer;
    sze: Int64;
begin
  ms := TMemoryStream.Create;
  Image1.Picture.Bitmap.SaveToStream(ms);
  ms.Position := 0;
  sze := ms.Size;
  GetMem(buff, sze);
  ms.Read(buff^, sze);
  ms.Clear;
  Image1.Picture.Bitmap.LoadFromStream(ms);
  Repaint;
  Image1.Repaint;
  MakeColorAnalysis(buff, sze);
  ms.Write(buff^, sze);
  ms.Position := 0;
  Image1.Picture.Bitmap.LoadFromStream(ms);
  ms.Free;
end;

ziolko.
0
 
ziolkoCommented:
of course there's no error checking, also on DLL side you should validate incoming parameters

ziolko.
0
 
ziolkoCommented:
oops should be:

  finally
    ms.Free;
    bmp.Free;  
  end;

ziolko
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ziolkoCommented:
damn I'm trying to post here faster than I think:)

procedure MakeColorAnalysis(Buffer: Pointer;var Size: Int64);stdcall;
var ms: TMemoryStream;
    bmp: TBitmap;
begin
  if Assigned(Buffer) and (Size > 0) then begin
    ms := TMemoryStream.Create;
    bmp := TBitmap.Create;
    try
      ms.Write(Buffer^, Size);
      ms.Position := 0;
      bmp.LoadFromStream(ms);
      bmp.Canvas.MoveTo(10, 10);
      bmp.Canvas.LineTo(100, 100);
      ms.Clear;
      bmp.SaveToStream(ms);
      Size := ms.Size;
      ms.Position := 0;
      ReallocMem(Buffer, Size);
      ms.Read(Buffer^, Size);
    finally
      ms.Free;
      bmp.Free;
    end;
  end;
end;


ziolko.
0
 
panJamesAuthor Commented:
It works, thank u very much ziolko :-)

I made small change here:

procedure TForm1.Button4Click(Sender: TObject);
var ms: TMemoryStream;
    buff: Pointer;
    sze: Int64;
begin
  ms := TMemoryStream.Create;
  Image1.Picture.Bitmap.SaveToStream(ms);
  ms.Position := 0;
  sze := ms.Size;
  GetMem(buff, sze);
  ms.Read(buff^, sze);

  {ms.Clear;
  Image1.Picture.Bitmap.LoadFromStream(ms);
  Repaint;
  Image1.Repaint;}
  MakeColorAnalysis2(buff, sze);
  ms.Clear; //new
  ms.Write(buff^, sze);
  ms.Position := 0;
  Image1.Picture.Bitmap.LoadFromStream(ms);

  ms.Free;
end;

II do not understand how it works (additional question if I may :-) ):

Within DLL:

      ReallocMem(Buffer, Size);
      ms.Read(Buffer^, Size); //why do we need it? I thought that Buffer is a Pointer pointing to
                                              //the beginning of the memory used to store Stream so
                                              //I thought that Pointer stores address of kind of ONE memory cell
                                              //not the whole memory stream, so continuing this way of thinking I    
                                              //expected that we do not need to change this pointer at all,
                                              //thought that MemoryStream starts in the same place in memory and only
                                              //it's lenght may change- any comment on these thoughts? :-)


Thank u very much

panJames
0
 
ziolkoCommented:
see what happens on EXE side:
  GetMem(buff, sze);
  ms.Read(buff^, sze);
we alloc memmory "under" buff and COPY contents of stream into buff then buff is passed to DLL

>>I thought that Pointer stores address of kind of ONE memory cell not the whole memory stream

nope, buffer stores whole contents of stream.

in other words. From EXE perspective we dump stream to buffer then let the DLL do whatever it's supposed to do when DLL is done reload buffer to stream and display in image.
from DLL perspective we got pointer to some buffer so we load it to stream do our job when done dump it back to buffer.

ReallocMem is required as after drawing on bitmap's canvas size of picture may be different than orginal

ziolko.
0
 
ziolkoCommented:
we could pass only pointer to streams data like this:

MakeColorAnalysis2(ms.Memory, sze);

but memorystream has properties like Size and Position which we can't update from DLL.

ziolko.
0
 
panJamesAuthor Commented:
Thank u very much for answers,
if possible please have a look at my next question:

'Passing a bitmap to DLL using handle to it'


Polon
0
All Courses

From novice to tech pro — start learning today.