?
Solved

Transfer a jpeg image from a ANSIString to a TImage

Posted on 2010-11-29
11
Medium Priority
?
831 Views
Last Modified: 2012-06-27
I have a TImage on a form
I fetch a blob from a database into an ANSIString
This is in fact a jpeg image that I know want to display via the TIMage
Currently I used a temporary file where I write the string and the use the TImage.Picture.LoadFromFile method
Is there a way without using a temp file ?
Sure there is via a Stream but I don't know how to proceed
A working code sample will be appreciated
Thanks
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
  • 3
  • 2
  • +2
11 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 34229997
0
 
LVL 24

Accepted Solution

by:
jimyX earned 1200 total points
ID: 34230014
I use this one from http://delphi.about.com/

uses jpeg;
...
procedure TForm1.btnShowImageClick(Sender: TObject);
var
  bS  : TADOBlobStream;
  Pic : TJpegImage;
begin
  bS := TADOBlobStream.Create(AdoTable1Picture, bmRead);
  try
    Pic:=TJpegImage.Create;
    try
     Pic.LoadFromStream(bS);
     ADOImage.Picture.Graphic:=Pic;
    finally
     Pic.Free;
    end;
  finally
    bS.Free
  end;
end;

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 34230075
You can also do this:

  aBlobStream: TStream;
  aCds: TClientDataSet;
  TheIMage: TIMage;

  aBlobStream := aCds.CreateBlobStream(aCds.FieldByName('TheBLOBFieldName', bmRead));
  if aBlobStream.Size > 0 then
    TImage.Picture.Bitmap.LoadFromStream(aBlobStream);

Just a Pseudo-Code!!!



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.

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34230356

procedure LoadImage();
var
  Stream: TMemoryStream
begin
  if not Dataset.FieldByName('FieldName).IsNull then
  begin
    Stream := TMemoryStream.Create;
    try
      (Dataset.FieldByName('FieldName') as TBlobField).SaveToStream(Stream);
      AStream.Seek(0, soFromBeginning);
      Image1.Picture.Bitmap.LoadFromStream(Stream);
    finally
      FreeAndNil(Stream);
    end;
  end;  
end;
0
 

Author Comment

by:LeTay
ID: 34230655
Hello all,
I should have described the question differently
I already have the JPEG data in a ANSIString variable and I have a routine to move it into a Stream
Based on jimyX, I coded this
 ST  := TStringStream.Create;
 JPG := TJPEGImage.Create;
 BTANSIString2Stream(Photo,ST); // this is my working function (Photo is the ANSIString)
 JPG.LoadFromStream(ST);
 F01.Image.Picture.Graphic := JPG;
 JPG.Free;
 ST.Free;

This works fine except at the ultimate moment it displays it on the screen : I get an exception
The stack trace follows :
0054379a +04a BriTayEx.exe jpeg               JpegError
00544edd +061 BriTayEx.exe jpeg               jpeg_consume_input
00544e3e +02e BriTayEx.exe jpeg               jpeg_read_header
00543b55 +0b1 BriTayEx.exe jpeg               InitDecompressor
005442c6 +04e BriTayEx.exe jpeg               TJPEGImage.GetBitmap
005440cf +00b BriTayEx.exe jpeg               TJPEGImage.Draw
0048ffce +02a BriTayEx.exe Graphics           TCanvas.StretchDraw
004b3a72 +0d2 BriTayEx.exe ExtCtrls 2616  +16 TImage.Paint

It says JPEG error #42 ...
0
 
LVL 25

Expert Comment

by:epasquier
ID: 34230751
you should use Assign
F01.Image.Picture.Assign( JPG );
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34230752
oops, JPeg, yap you have to use TJPegImage class as jimmyx shows

procedure LoadImage();
var
  Stream: TMemoryStream
begin
  if not Dataset.FieldByName('FieldName).IsNull then
  begin
    Stream := TMemoryStream.Create;
    try
      (Dataset.FieldByName('FieldName') as TBlobField).SaveToStream(Stream);
      Stream.Seek(0, soFromBeginning);
     Image := TJpegImage.Create;
     try      
       Image.LoadFromStream(Stream);
       Image1.Picture.Graphic := Image;
      finally
       FreeAndNil(Image);
      end;
     finally
      FreeAndNil(Stream);
    end;
  end;  
end;
0
 

Author Comment

by:LeTay
ID: 34230800
Using assign gives the same exception JPEG #42

For ewangoya : I have the JPEG in a ANSIString (not reading in the database at that stage)
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 800 total points
ID: 34230822

Why don't you use

ST  := TStringStream.Create(Photo);

whats in your
BTANSIString2Stream procedure
0
 

Author Comment

by:LeTay
ID: 34230894
Here is the procedure
procedure BTANSIString2Stream(var S:ANSIstring;var ST:TStringStream);
begin
 ST.Seek(0,soFromBeginning);
 if (Length(S) <> 0)
  then ST.Write(S[1],Length(S))
  else;
end;
Using ST  := TStringStream.Create(Photo); is better in my context and it works
But it now also works with the procedure above, provided I add a final statement :
 ST.Seek(0,soFromBeginning);
Many thanks !
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34230937
Read from the database directly into the Stream and then move it to the Image:

var
  JPG: TJPEGImage;
  ST: TStream;
begin
  if (not YourTable.FieldByName('Photo').IsNull) then
    begin
      ST := YourTable.CreateBlobStream(YourTable.FieldByName('Photo'),bmRead);
      JPG := TJPEGImage.Create;
      try
        JPG.LoadFromStream(ST);
        Photo.Picture.Assign(JPG);
        Photo.Visible := True;
      finally
        JPG.Free;
        ST.Free;
      end;
    end
  else
    Photo.Visible := False;

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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