Advertisement

05.06.2008 at 03:16AM PDT, ID: 23378936
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Graphic.SaveToStream - Takes morethan 600ms in Delphi

Tags: Delphi, Ver 6
Hi Experts,
I'm facing a strange problem in OCX written in Delphi 6.
What I'm trying to do is to send a Jpeg image to the host as array of Variant.

When I time the following code, Graphic.SaveToStream(strmMem) takes about 600 ms. Is there anything wrong in the way I'm code?

function TPCSTrioPRMV2.GraphicToVar(Graphic: TGraphic): Variant;
var
 strmMem : TMemoryStream;
 lpMem   : Pointer;
begin
  strmMem := TMemoryStream.Create; // Create stream for persisting graphic
  try // Resource protection
    Graphic.SaveToStream(strmMem); // Save graphic to stream
    TVariantArg(result).vt:=VT_UI1 or VT_ARRAY; // Set result vt type
    TVariantArg(result).parray := SafeArrayCreateVector(VT_UI1, 0, strmMem.Size); // Create safearray for result
    lpMem:= VarArrayLock(result); // Lock the array
    try      // Resource protection
       Move(strmMem.Memory^, lpMem^, strmMem.Size); // Move memory from the stream to the array
    finally
       VarArrayUnlock(result); // Unlock the array
    end;
  finally
    strmMem.Free; // Free the stream
  end;
end;
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: pcssecure
Solution Provided By: ziolko
Participating Experts: 1
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
05.06.2008 at 04:15AM PDT, ID: 21506235

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 04:27AM PDT, ID: 21506294

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 07:24PM PDT, ID: 21512747

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 08:11PM PDT, ID: 21512922

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 09:56PM PDT, ID: 21513266

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 12:08AM PDT, ID: 21513737

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 02:42PM PDT, ID: 21536566

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 03:21AM PDT, ID: 21545662

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:37AM PDT, ID: 21547355

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:50AM PDT, ID: 21547468

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:58AM PDT, ID: 21547522

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 08:11AM PDT, ID: 21547649

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 08:18AM PDT, ID: 21547721

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 10:01PM PDT, ID: 21552438

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.06.2008 at 04:15AM PDT, ID: 21506235

Rank: Sage

how big is your jpeg file?

ziolko.
 
05.06.2008 at 04:27AM PDT, ID: 21506294

Rank: Sage

I did this on 512KB JPEG

procedure TForm1.Button6Click(Sender: TObject);
var ms: TMemoryStream;
    ov: OleVariant;
    lck: Pointer;
    tc: Cardinal;
begin
  tc := GetTickCount;
  ms := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(ms);
    ms.Position := 0;
    ov := VarArrayCreate([0, ms.Size], varByte);
    lck := VarArrayLock(ov);
    try
      ms.Read(lck^, ms.Size);
      ms.SaveToFile('C:\test.jpg');
    finally
      VarArrayUnlock(ov);
    end;
  finally
    ms.Free;
  end;
  ShowMessage(IntToStr(GetTickCount - tc));
end;

GetTickCount - tc was 0

ziolko.
 
05.06.2008 at 07:24PM PDT, ID: 21512747
Hi ziolko,
its about 600KB only. So what is going wrong with the way I do?

Regards
-Mani
 
05.06.2008 at 08:11PM PDT, ID: 21512922
Hi ziolko,
Strangely, when I ran my code within an application it also gave me 0 tickcount. So whats going on within ActiveX environment?

Thanks
 
05.06.2008 at 09:56PM PDT, ID: 21513266

Rank: Sage

hi,

I see no reason there might be any difference in ActiveX. I'll make a simple demo and take a look at this

ziolko.
 
05.07.2008 at 12:08AM PDT, ID: 21513737

Rank: Sage

I did some testing and there's no difference if this code is run from within application or COM library. below my test code.

interface declaration in OCX:

  IArrayStream = interface(IUnknown)
    ['{D01A755C-A36D-44CC-810A-8ED7DEC11D84}']
    function SetJPEG(Stream: OleVariant): HResult; stdcall;
    function GetJPEG: OleVariant; stdcall;
  end;

implementation in COM DLL:

unit uArrayStream;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, ActiveX, Classes, ComObj, OCXLib_TLB, StdVcl;

type
  TArrayStream = class(TTypedComObject, IArrayStream)
  protected
    function SetJPEG(Stream: OleVariant): HResult; stdcall;
    function GetJPEG: OleVariant; stdcall;
    {Declare IArrayStream methods here}
  end;

implementation

uses ComServ, Variants;

function TArrayStream.SetJPEG(Stream: OleVariant): HResult;
var ms: TMemoryStream;
    lck: Pointer;
begin
  Result := E_FAIL;
  ms := TMemoryStream.Create;
  try
    if VarIsArray(Stream) then begin
      lck := VarArrayLock(Stream);
      try
        ms.Write(lck^, VarArrayHighBound(Stream, 1));
      finally
        VarArrayUnlock(Stream);  
      end;
      ms.SaveToFile('D:\MyTestProjects\EE\bin\ocx_set.jpg');
      Result := S_OK;
    end;
  finally
    ms.Free;
  end;
end;

function TArrayStream.GetJPEG: OleVariant;
var ms: TMemoryStream;
    lck: Pointer;
begin
  Result := Unassigned;
  ms := TMemoryStream.Create;
  try
    ms.LoadFromFile('D:\MyTestProjects\EE\bin\screen.jpg');
    ms.Position := 0;
    Result := VarArrayCreate([0, ms.Size], varByte);  
    lck := VarArrayLock(Result);
    try
      ms.Read(lck^, ms.Size);  
    finally
      VarArrayUnlock(Result);
    end;
  finally
    ms.Free;
  end;
end;

initialization
  TTypedComObjectFactory.Create(ComServer, TArrayStream, Class_ArrayStream,
    ciMultiInstance, tmApartment);
end.


usage on EXE side:

procedure TForm1.Button4Click(Sender: TObject);
var arr: IArrayStream;
    ov: OleVariant;
    lck: Pointer;
    ms: TMemoryStream;
    tc: Cardinal;
begin
  tc := GetTickCount;
  arr := CoArrayStream.Create;
  ms := TMemoryStream.Create;  
  try
    ov := arr.GetJPEG;
    if VarIsArray(ov) then begin
      lck := VarArrayLock(ov);
      try
        ms.Write(lck^, VarArrayHighBound(ov, 1));  
      finally
        VarArrayUnlock(ov);
      end;
      ms.SaveToFile('D:\MyTestProjects\EE\bin\test.jpg');
    end;
  finally
    arr := nil;
    ms.Free;
  end;
  Memo1.Lines.Add(IntToStr(GetTickCount - tc));  
end;

procedure TForm1.Button6Click(Sender: TObject);
var ms: TMemoryStream;
    arr: IArrayStream;
    ov: OleVariant;
    lck: Pointer;
    tc: Cardinal;
begin
  tc := GetTickCount;
  arr := CoArrayStream.Create;  
  ms := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(ms);
    ms.Position := 0;
    ov := VarArrayCreate([0, ms.Size], varByte);
    lck := VarArrayLock(ov);
    try
      ms.Read(lck^, ms.Size);
    finally
      VarArrayUnlock(ov);
    end;
    arr.SetJPEG(ov);
  finally
    ms.Free;
    arr := nil;
  end;
  Memo1.Lines.Add(IntToStr(GetTickCount - tc));
end;


ziolko
Accepted Solution
 
05.09.2008 at 02:42PM PDT, ID: 21536566

Rank: Sage

so how it's going?

ziolko.
 
05.12.2008 at 03:21AM PDT, ID: 21545662
Hi ziolko, Thanks for your all help. I'm still stuck.

If I use LoadfromFile in my code then it is fast at no time.
But if I use TJPEGImage, the problem surfaced at myJpg.SaveToStream(ms); 300ms.

  myJpg := TJPEGImage.Create;
  myJpg.Assign(myImg);
  ms := TMemoryStream.Create;
  myJpg.SaveToStream(ms);  // if i use ms.LoadFromFile('c:\image1.jpg') it is really fast (0 secs).

 where myImg is of TJPEGImage type.

Any idea wats going wrong here?

Thanks.
 
05.12.2008 at 07:37AM PDT, ID: 21547355

Rank: Sage

that's really strange operation on memory (savetostream) should be faster than reading from disk (loadfromfile), I don't have jpeg.pas on my D7 installation so can't see what they do.

ziolko.
 
05.12.2008 at 07:50AM PDT, ID: 21547468
Hi ziolko,
You want me to send that piece of code from jpeg.pas? I desperate for a fix.
If not, any alternate to save and load the Jpeg Image.
I can't Write anything to the HD at runtime and I need to find a solution to transfer the image fater.
How about memory mapped files? Can I use them like disk file?

Regards
 
05.12.2008 at 07:58AM PDT, ID: 21547522

Rank: Sage

yeah you can attach whole jpeg.pas here (just change extension to .txt),
eventually if disk I/O is faster then working with streams you can try:

jpeg.SavetoFile()
ms.LoadFromFile()

instead: jpeg.SaveToStream(ms)

one more thing did you try on different machines? maybe it's something with your PC (high usage of RAM?)
try to reproduce problem on different machines.

memory mapped file? it's also operation on memory so I would be surprised if it was faster then SaveToStream()

ziolko.
 
05.12.2008 at 08:11AM PDT, ID: 21547649
Sorry ziolko
I only have dcu file.

Please note that I can't access HD during runtime. It is read only. So I can't save and load from file.
I've tried on different PC and the results are the same.

Regards
 
05.12.2008 at 08:18AM PDT, ID: 21547721

Rank: Sage

I really have no idea... this jpg is displayed somewhere or maybe some oprations are done on image while you trying to save it to stream?

ziolko.
 
05.12.2008 at 10:01PM PDT, ID: 21552438

Rank: Sage

>>the bmp size is 50 times bigger than Jpeg

maybe it's compression/quality thing that jpeg is doing this while saving to stream?

ziolko.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628