[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to copy TJPEGImage

Posted on 2013-02-03
6
Medium Priority
?
589 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 28

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
Industry Leaders: 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!

 

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 28

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

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.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

649 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