LeTay
asked on
Transfer a jpeg image from a ANSIString to a TImage
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.LoadFromFil e 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
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.LoadFromFil
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can also do this:
aBlobStream: TStream;
aCds: TClientDataSet;
TheIMage: TIMage;
aBlobStream := aCds.CreateBlobStream(aCds .FieldByNa me('TheBLO BFieldName ', bmRead));
if aBlobStream.Size > 0 then
TImage.Picture.Bitmap.Load FromStream (aBlobStre am);
Just a Pseudo-Code!!!
aBlobStream: TStream;
aCds: TClientDataSet;
TheIMage: TIMage;
aBlobStream := aCds.CreateBlobStream(aCds
if aBlobStream.Size > 0 then
TImage.Picture.Bitmap.Load
Just a Pseudo-Code!!!
procedure LoadImage();
var
Stream: TMemoryStream
begin
if not Dataset.FieldByName('Field
begin
Stream := TMemoryStream.Create;
try
(Dataset.FieldByName('Fiel
AStream.Seek(0, soFromBeginning);
Image1.Picture.Bitmap.Load
finally
FreeAndNil(Stream);
end;
end;
end;
ASKER
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 ...
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,
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 ...
you should use Assign
F01.Image.Picture.Assign( JPG );
F01.Image.Picture.Assign( JPG );
oops, JPeg, yap you have to use TJPegImage class as jimmyx shows
procedure LoadImage();
var
Stream: TMemoryStream
begin
if not Dataset.FieldByName('Field Name).IsNu ll then
begin
Stream := TMemoryStream.Create;
try
(Dataset.FieldByName('Fiel dName') as TBlobField).SaveToStream(S tream);
Stream.Seek(0, soFromBeginning);
Image := TJpegImage.Create;
try
Image.LoadFromStream(Strea m);
Image1.Picture.Graphic := Image;
finally
FreeAndNil(Image);
end;
finally
FreeAndNil(Stream);
end;
end;
end;
procedure LoadImage();
var
Stream: TMemoryStream
begin
if not Dataset.FieldByName('Field
begin
Stream := TMemoryStream.Create;
try
(Dataset.FieldByName('Fiel
Stream.Seek(0, soFromBeginning);
Image := TJpegImage.Create;
try
Image.LoadFromStream(Strea
Image1.Picture.Graphic := Image;
finally
FreeAndNil(Image);
end;
finally
FreeAndNil(Stream);
end;
end;
end;
ASKER
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)
For ewangoya : I have the JPEG in a ANSIString (not reading in the database at that stage)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 !
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
But it now also works with the procedure above, provided I add a final statement :
ST.Seek(0,soFromBeginning)
Many thanks !
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;
TImage.LoadFromStream
http://www.delphipraxis.net/117340-blob-per-stream-aus-db-lesen-und-imageview-anzeigen.html