• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

How to copy TJPEGImage

In Delphi, how can I copy the image from one TJPEGImage to another ?
Example, I have this
var
 A,B:TJPEGImage;
.../...
begin
.../...
// A has been loaded somewhere and how I want to copy the image to B
0
LeTay
Asked:
LeTay
  • 3
  • 2
1 Solution
 
Ephraim WangoyaCommented:
Assign it to B

B.Assign(A)
0
 
LeTayAuthor Commented:
Will the assign really copy the data or just give a pointer ?
I need the data to be copied...
0
 
Sinisa VukCommented:
This comment line is taken from source of jpeg and give answer to upper question:
// FImage is shared if Assign is used to copy between TJPEGImage instances

Simply create new intermediate bitmap and do assign with it:

...
var
  jpg2: TJPEGImage;
  Bmp: TBitmap;
begin
  jpg2 := TJPEGImage.Create;
  Bmp := TBitmap.Create;
  Bmp.Assign(Jpg1);
  Jpg2.Assign(Bmp);
....
  Bmp.Free;
  jpg2.Free;
...

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LeTayAuthor Commented:
Just using Assign crashed my exe (access violation)
The TJPEGImage B (destinator) is inside a DLL ...
0
 
LeTayAuthor Commented:
Isnt' there a way to copy the picture from one TJPEGImage to another using a TMemoryStream and, if yes, how ?
Thanks
0
 
Sinisa VukCommented:
Yes, this can be solution - but please note - dll and main application must be compiled with same version of delphi if you will transfer TMemoryStream class. Better solution is to pass pointer to memory location like here:

DLL code:
function AssignJpeg(pMemory: Pointer; sz: Integer): Integer; stdcall;
var
  ms: TMemoryStream;
begin
  Result := 0;
  A.LoadFromfile(...); //is your TJpegImage  

  ms:=TMemoryStream.Create;
  try
    A.SaveToStream(ms);
    ms.Seek(0,0);
    if sz<ms.Size then Exit; //not enough allocated memory for transfer

    CopyMemory(ms.Memory, pMemory, ms.Size);
    Result := ms.Size; //return transfered bytes
  finally
    ms.Free;
  end;
end;

Open in new window


App code:
procedure GetJpegFromDll;
var
  ms: TMemoryStream;
  n: Integer;
begin
  ms:=TMemoryStream.Create;
  try
    ms.SetSize(20000); //allocate enough memory
    ms.Seek(0,0);
   
    n := AssignJpeg(ms.Memory, ms.Size);  //dll exported function
    if n>0 then
    begin
      ms.SetSize(n); //truncate rest
      B.LoadFromStream(ms); //assign to image
    end;
  finally
    ms.Free;
  end;
end;

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now