Solved

How to copy TJPEGImage

Posted on 2013-02-03
6
561 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
Comment Utility
Assign it to B

B.Assign(A)
0
 

Author Comment

by:LeTay
Comment Utility
Will the assign really copy the data or just give a pointer ?
I need the data to be copied...
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

Author Comment

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

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now