panJames
asked on
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.B itmap.Canv as.PenPos := Point(0, 0);
TImage(aPImage^).Picture.B itmap.Canv as.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 :-)
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
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.B
TImage(aPImage^).Picture.B
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 :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops should be:
finally
ms.Free;
bmp.Free;
end;
ziolko
finally
ms.Free;
bmp.Free;
end;
ziolko
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.
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.
ASKER
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.Save ToStream(m s);
ms.Position := 0;
sze := ms.Size;
GetMem(buff, sze);
ms.Read(buff^, sze);
{ms.Clear;
Image1.Picture.Bitmap.Load FromStream (ms);
Repaint;
Image1.Repaint;}
MakeColorAnalysis2(buff, sze);
ms.Clear; //new
ms.Write(buff^, sze);
ms.Position := 0;
Image1.Picture.Bitmap.Load FromStream (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
I made small change here:
procedure TForm1.Button4Click(Sender
var ms: TMemoryStream;
buff: Pointer;
sze: Int64;
begin
ms := TMemoryStream.Create;
Image1.Picture.Bitmap.Save
ms.Position := 0;
sze := ms.Size;
GetMem(buff, sze);
ms.Read(buff^, sze);
{ms.Clear;
Image1.Picture.Bitmap.Load
Repaint;
Image1.Repaint;}
MakeColorAnalysis2(buff, sze);
ms.Clear; //new
ms.Write(buff^, sze);
ms.Position := 0;
Image1.Picture.Bitmap.Load
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
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.
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.
we could pass only pointer to streams data like this:
MakeColorAnalysis2(ms.Memo ry, sze);
but memorystream has properties like Size and Position which we can't update from DLL.
ziolko.
MakeColorAnalysis2(ms.Memo
but memorystream has properties like Size and Position which we can't update from DLL.
ziolko.
ASKER
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
if possible please have a look at my next question:
'Passing a bitmap to DLL using handle to it'
Polon
ziolko.