?
Solved

How to copy TJPEGImage

Posted on 2013-02-03
6
Medium Priority
?
584 Views
Last Modified: 2013-02-21
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
Comment
Question by:LeTay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 38848909
Assign it to B

B.Assign(A)
0
 

Author Comment

by:LeTay
ID: 38848974
Will the assign really copy the data or just give a pointer ?
I need the data to be copied...
0
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 38849261
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:LeTay
ID: 38854232
Just using Assign crashed my exe (access violation)
The TJPEGImage B (destinator) is inside a DLL ...
0
 

Author Comment

by:LeTay
ID: 38854237
Isnt' there a way to copy the picture from one TJPEGImage to another using a TMemoryStream and, if yes, how ?
Thanks
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 38854373
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question