Solved

How to copy TJPEGImage

Posted on 2013-02-03
6
564 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
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:ewangoya
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 26

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 26

Accepted Solution

by:
Sinisa Vuk earned 500 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 220
Show Listview image from database (String field) 5 127
Working with hours 3 58
Breakpoint doesn't stop in my variable 3 28
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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