This question already has answers here:
How to insert image into database using TADOQuery Component Only
(2 answers)
Store images in MS-Access Database using Delphi6
(1 answer)
Closed 10 years ago.
I'm using this code to load images into my Timage:
begin
if OpenPictureDialog1.Execute(Self.Handle) then
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
end;
Then I'm using this code to store into my ms access database:
var
AStream : TMemoryStream;
begin
Adotable1.Append;
AStream := TMemoryStream.Create;
try
Image1.Picture.Graphic.SaveToStream(AStream);
AStream.Position := 0;
if Adotable1.Active then
begin
TBlobField(Adotable1.FieldByName('Termograma')).LoadFromStream(AStream);
end;
finally
AStream.Free;
end;
adotable1.Post;
But now I want to display these saved images on a Timage, can anyone help me?
The images are .jpeg format
As far as TPicture is not able to decide which kind of TGraphic it has to create for loading, since a stream does not have an extension like a filename you have to decide it, and assign the Graphic.
In this case a TJPEGImage to the Picture.
var
JPG:TJPEGImage;
ms:TMemoryStream;
begin
JPG:=TJPEGImage.Create;
ms:=TMemoryStream.Create;
try
TBlobField(AdoTable1.FieldByName('Termograma')).SaveToStream(ms);
ms.Position := 0;
JPG.LoadFromStream(ms);
Image2.Picture.Assign(JPG);
finally
JPG.Free;
ms.Free;
end;
end;
The following unit is able to store diffent graphicformats in blobfields.
Storage is not compatible to simple storing of image data because information about then graphicformat is stored too, to give the ability to create the needed class for loading.
unit LoadSaveImageBlobs;
// 20120224 by Thomas Wassermann
// Adapt. RegisterClasses and uses for your requirements
// based on an Idea of Emiliano Sos
interface
uses Classes,DB,Graphics,Jpeg,PngImage;
Procedure SavePicture2Blob(Blob: TBlobField; Picture: TPicture);
Procedure LoadPictureFromBlob(Picture: TPicture; Blob: TBlobField);
implementation
Procedure SavePicture2Blob(Blob: TBlobField; Picture: TPicture);
var
ms, ms2: TMemoryStream;
theClassName: AnsiString;
len: Byte;
begin
ms := TMemoryStream.Create;
try
Blob.Clear;
theClassName := Picture.Graphic.ClassName;
len := Length(theClassName);
ms.WriteBuffer(len, 1);
if len > 0 then
ms.WriteBuffer(theClassName[1], len);
ms2 := TMemoryStream.Create;
try
Picture.Graphic.SaveToStream(ms2);
ms2.Position := 0;
if ms2.Size > 0 then
ms.CopyFrom(ms2, ms2.Size);
finally
ms2.Free;
end;
Blob.LoadFromStream(ms);
finally
ms.Free;
end;
end;
Procedure LoadPictureFromBlob(Picture: TPicture; Blob: TBlobField);
var
ms, ms2: TMemoryStream;
len: Byte;
theClassName: AnsiString;
Graphic: TGraphic;
GraphicClass: TGraphicClass;
begin
ms := TMemoryStream.Create;
Blob.SaveToStream(ms);
ms.Position := 0;
try
ms.ReadBuffer(len, 1);
SetLength(theClassName, len);
if len > 0 then
ms.ReadBuffer(theClassName[1], len);
GraphicClass := TGraphicClass(FindClass(theClassName));
if (GraphicClass <> nil) and (len > 0) then
begin
Graphic := GraphicClass.Create;
ms2 := TMemoryStream.Create;
try
ms2.CopyFrom(ms, ms.Size - len - 1);
ms2.Position := 0;
Graphic.LoadFromStream(ms2);
finally
ms2.Free;
end;
Picture.Assign(Graphic);
end;
finally
ms.Free;
end;
end;
initialization
// you might register others if wished
RegisterClasses([TIcon, TMetafile, TBitmap, TJPEGImage,TPngImage]);
end.
Related
i have a TMemoryStream of a picture but i want to add a String inside that TMemoryStream that defines what that picture is then read them separately.
the TMemoryStream will be sent from IdTCPclient to IdTCPserver and server will read the string then the picture.
id prefer if the string was in the beginning of the stream.
it is my first encounter on dealing with memory stream with multiple date inside it, please enlighten me.
//Edit: adding current code based on MBo Answer
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
bm: TBitmap;
ms ,ms1: TMemoryStream;
len: Int64;
JPEGImage: TJPEGImage;
begin
IdTCPClient1.Connect;
begin
ms:=TMemoryStream.Create;
ms1:=TMemoryStream.Create;
JPEGImage := TJPEGImage.Create;
s := 'A nice picture';
ms:=CapScreen(100); //function result in memory stream
len := Length(s) * SizeOf(Char);
ms1.Write(len, SizeOf(len));
ms1.Write(PChar(s)^, len);
ms1.copyfrom(ms,ms.Size) ; // <-- stream read error
ms1.Position := 0;
Caption := s;
JPEGImage.LoadFromStream(ms1);
Image1.Picture.Assign(JPEGImage);
end;
IdTCPClient1.IOHandler.Write(ms1, 0, True);
ms.Free;
ms1.Free;
JPEGImage.Free;
end;
Simple example to start from. Picture is loaded from disk here
var
s: string;
bm: TBitmap;
ms: TMemoryStream;
len: Int64;
begin
ms := TMemoryStream.Create;
bm := TBitmap.Create;
try
bm.LoadFromFile('d:\d.bmp');
//write string body size, body itself
s := 'A nice picture';
len := Length(s) * SizeOf(Char);
ms.Write(len, SizeOf(len));
ms.Write(PChar(s)^, len);
//now picture
bm.SaveToStream(ms);
//change string and picture to be sure we load new ones
bm.Canvas.FillRect(rect(0,0,100,100));
s := '';
//now restore and show
ms.Position := 0;
ms.Read(len, sizeof(len));
SetLength(s, len div SizeOf(Char));
ms.Read(PChar(s)^, len);
Caption := s;
bm.LoadFromStream(ms); //reads picture from current position
Canvas.Draw(0, 0, bm);
finally
ms.Free;
bm.Free;
end;
Example for jpeg loaded from another stream:
var
s: string;
ms, jpstream: TMemoryStream;
len: Int64;
jp: TJpegImage;
begin
ms := TMemoryStream.Create;
jpstream := TMemoryStream.Create;
jp := TJpegImage.Create;
try
//write string body size, body itself
s := 'A nice picture';
len := Length(s) * SizeOf(Char);
ms.Write(len, SizeOf(len));
ms.Write(PChar(s)^, len);
jpstream.LoadFromFile('d:\d.jpg');
jpstream.Position := 0;
ms.CopyFrom(jpstream, jpstream.Size);
//now restore ans show
ms.Position := 0;
ms.Read(len, sizeof(len));
SetLength(s, len div SizeOf(Char));
ms.Read(PChar(s)^, len);
Caption := s;
jp.LoadFromStream(ms);
Canvas.Draw(0, 0, jp);
finally
ms.Free;
jp.Free;
jpstream.Free;
end;
I've got a minor issue with decompressing using the ZLib unit in Delphi
unit uZCompression;
interface
uses
uCompression;
type
TZZipCompression = class(TInterfacedObject, ICompression)
public
function DoCompression(aContent: TArray<Byte>): TArray<Byte>;
function DoDecompression(aContent: TArray<Byte>): TArray<Byte>;
function GetWindowsBits: Integer; virtual;
end;
TZGZipCompression = class(TZZipCompression)
function GetWindowsBits: Integer; override;
end;
implementation
uses
System.ZLib, System.Classes, uMxKxUtils;
{ TZCompression }
function TZZipCompression.DoCompression(aContent: TArray<Byte>): TArray<Byte>;
var
LContentStream, LOutputStream: TMemoryStream;
LCompressedStream: TZCompressionStream;
begin
LContentStream := ByteArrayToStream(aContent);
LOutputStream := TMemoryStream.Create;
LCompressedStream := TZCompressionStream.Create(LOutputStream, zcDefault, GetWindowsBits);
LCompressedStream.CopyFrom(LContentStream, LContentStream.Size);
LCompressedStream.Free;
Result := StreamToByteArray(LOutputStream);
LOutputStream.Free;
LContentStream.Free;
end;
function TZZipCompression.DoDecompression(aContent: TArray<Byte>): TArray<Byte>;
var
LContentStream, LOutputStream: TMemoryStream;
LDecompressedStream: TZDecompressionStream;
begin
LContentStream := ByteArrayToStream(aContent);
LOutputStream := TMemoryStream.Create;
LDecompressedStream := TZDecompressionStream.Create(LContentStream);
LOutputStream.CopyFrom(LDecompressedStream, LDecompressedStream.Size);
LDecompressedStream.Free;
Result := StreamToByteArray(LOutputStream);
LOutputStream.Free;
LContentStream.Free;
end;
function TZZipCompression.GetWindowsBits: Integer;
begin
Result := 15;
end;
{ TZGZipCompression }
function TZGZipCompression.GetWindowsBits: Integer;
begin
Result := inherited;
Result := Result + 16;
end;
end.
This is my unit which is driven by an interface (which you don't need to know about), and data is passed in and out through a TArray variables.
I've coded it to be able to do 2 types of compression, standard zip and gzip which is determined by the windowsbits passed in to the functions.
Here are a couple of other functions being used to convert the TArray to TMemoryStream
function ByteArrayToStream(aContent: TArray<Byte>): TMemoryStream;
begin
Result := TMemoryStream.Create;
Result.Write(aContent, length(aContent)*SizeOf(aContent[0]));
Result.Position := 0;
end;
function StreamToByteArray(aStream: TMemoryStream): TArray<Byte>;
var
LStreamPos: Int64;
begin
if Assigned(aStream) then
begin
LStreamPos := aStream.Position;
aStream.Position := 0;
SetLength(Result, aStream.Size);
aStream.Read(Result, aStream.Size);
aStream.Position := LStreamPos;
end
else
SetLength(Result, 0);
end;
Now I can compress and decompress to .zip using the TZZipCompression class perfectly fine (it doesn't open up as a zip file, but it does decompress back to the original file which I can open and edit).
I can also compress to .gz using the TZGZipCompression class fine as well (interestingly I can open this gzip file perfectly well).
My issue however is that it won't decompress back from the .gz file and throws and error as soon as it hits
LOutputStream.CopyFrom(LDecompressedStream, LDecompressedStream.Size)
Funnily enough the Help file example has it as below
LOutputStream.CopyFrom(LDecompressedStream, 0)
But this doesn't work either.
Can anyone spot the issue?
Your conversion functions between TArray<Byte> and TMemoryStream are wrong, as you are not accessing the array content correctly. TArray is a dynamic array. When calling TMemoryStream.Write() and TMemoryStream.Read(), you are passing the memory address of the TArray itself, not the memory address of the data that the TArray points at. You need to reference the TArray to get the correct memory address, eg:
function ByteArrayToStream(const aContent: TArray<Byte>): TMemoryStream;
begin
Result := TMemoryStream.Create;
try
if Length(aContent) > 0 then
Result.WriteBuffer(aContent[0], Length(aContent));
Result.Position := 0;
except
Result.Free;
raise;
end;
end;
function StreamToByteArray(aStream: TMemoryStream): TArray<Byte>;
begin
if Assigned(aStream) then
begin
SetLength(Result, aStream.Size);
if Length(Result) > 0 then
Move(aStream.Memory^, Result[0], aStream.Size);
end
else
SetLength(Result, 0);
end;
Alternatively:
function ByteArrayToStream(const aContent: TArray<Byte>): TMemoryStream;
begin
Result := TMemoryStream.Create;
try
Result.WriteBuffer(PByte(aContent)^, Length(aContent));
Result.Position := 0;
except
Result.Free;
raise;
end;
end;
function StreamToByteArray(aStream: TMemoryStream): TArray<Byte>;
begin
if Assigned(aStream) then
begin
SetLength(Result, aStream.Size);
Move(aStream.Memory^, PByte(Result)^, aStream.Size);
end
else
SetLength(Result, 0);
end;
That being said, you don't need to waste memory making copies of the array data using TMemoryStream. You can use TBytesStream instead (since dynamic arrays are reference counted), eg:
function TZZipCompression.DoCompression(aContent: TArray<Byte>): TArray<Byte>;
var
LContentStream, LOutputStream: TBytesStream;
LCompressedStream: TZCompressionStream;
begin
LContentStream := TBytesStream.Create(aContent);
try
LOutputStream := TBytesStream.Create(nil);
try
LCompressedStream := TZCompressionStream.Create(LOutputStream, zcDefault, GetWindowsBits);
try
LCompressedStream.CopyFrom(LContentStream, 0);
finally
LCompressedStream.Free;
end;
Result := Copy(LOutputStream.Bytes, 0, LOutputStream.Size);
finally
LOutputStream.Free;
end;
finally
LContentStream.Free;
end;
end;
function TZZipCompression.DoDecompression(aContent: TArray<Byte>): TArray<Byte>;
var
LContentStream, LOutputStream: TBytesStream;
LDecompressedStream: TZDecompressionStream;
begin
LContentStream := TBytesStream.Create(aContent);
try
LOutputStream := TBytesStream.Create(nil);
try
LDecompressedStream := TZDecompressionStream.Create(LContentStream, GetWindowsBits);
try
LOutputStream.CopyFrom(LDecompressedStream, 0);
finally
LDecompressedStream.Free;
end;
Result := Copy(LOutputStream.Bytes, 0, LOutputStream.Size);
finally
LOutputStream.Free;
end;
finally
LContentStream.Free;
end;
end;
I need to access binary image data using XMLHttpRequest in Delphi. I am using the following code but its not working, could someone please tell me what's wrong with this code, thanks in advance.
//I am using this function to get Image Binary data into Memory Stream.
procedure SendGETRequest(p_Url: string; p_resStream: TMemoryStream);
begin
FXmlHttpReq.open(METHOD_GET, p_Url, false, FUsername, FPassword);
FXmlHttpReq.setRequestHeader(HTTP_AUTHENTICATION, HTTP_BASIC + EncodeBase64(
FUsername + ':'+FPassword));
FXmlHttpReq.setRequestHeader(HTTP_CACHE_CONTROL, HTTP_NO_CACHE);
//FXmlHttpReq.setRequestHeader('Content-type','application/octet-stream');
FXmlHttpReq.send('');
if not VarIsEmpty(FXmlHttpReq.responseBody) then
begin
p_resStream:= OleVariantToMemoryStream(FXmlHttpReq.responseStream);
end;//if...
end;
function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
Result.Free;
Result := nil;
end;
end;
responseStream is IStream. You need to convert it using TOleStream (AxCtrls):
uses AxCtrls, ComObj, ActiveX;
procedure TForm1.Button1Click(Sender: TObject);
var
oXMLHTTP: OleVariant;
MemoryStream: TMemoryStream;
Stream: IStream;
OleStream: TOleStream;
begin
oXMLHTTP := CreateOleObject('MSXML2.XMLHTTP.3.0');
oXMLHTTP.open('GET', 'https://www.google.com/images/srpr/logo11w.png', False);
oXMLHTTP.send(EmptyParam);
Stream := IUnknown(oXMLHTTP.ResponseStream) as IStream;
OleStream := TOleStream.Create(Stream);
try
OleStream.Position := 0;
MemoryStream := TMemoryStream.Create;
try
MemoryStream.CopyFrom(OleStream, OleStream.Size);
MemoryStream.SaveToFile('logo11w.png');
finally
MemoryStream.Free;
end;
finally
OleStream.Free;
end;
end;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This code give Access violation at the TBlobField line:
procedure TfrmCapture.Button3Click(Sender: TObject);
var
j: TJPEGImage;
S: TFileStream;
begin
J := TJPEGImage.Create;
try
J.Assign(Image1.Picture.Graphic);
J.CompressionQuality := 80;
J.Compress;
J.SaveToStream(S);
S.Position := 0;
TBlobField(frmSignout.tblImg.FieldByName('Picture')).LoadFromStream(S);
finally
J.Free;
end;
Image1.Picture := nil;
Close;
end;
Basically Im trying to convert a Timage into JPEG and then save it to Image field (MSSQL express)
any ideas how to fix it ?
You are not instantiating the TFileStream that you save to:
procedure TfrmCapture.Button3Click(Sender: TObject);
var
J: TJPEGImage;
S: TFileStream;
begin
J := TJPEGImage.Create;
try
J.Assign(Image1.Picture.Graphic);
J.CompressionQuality := 80;
J.Compress;
S := TFileStream.Create('c:\path to\somefile.jpg', fmCreate); // <-- add this!
try
J.SaveToStream(S);
S.Position := 0;
TBlobField(frmSignout.tblImg.FieldByName('Picture')).LoadFromStream(S);
finally
S.Free; // <-- add this
end;
finally
J.Free;
end;
Image1.Picture := nil;
Close;
end;
That being said, I would suggest avoiding the file altogether (unless you really need it) and use TDataSet.CreateBlobStream() instead of TBlobField.LoadFromStream():
procedure TfrmCapture.Button3Click(Sender: TObject);
var
J: TJPEGImage;
S: TStream;
begin
J := TJPEGImage.Create;
try
J.Assign(Image1.Picture.Graphic);
J.CompressionQuality := 80;
J.Compress;
S := frmSignout.tlbImg.CreateBlobStream(frmSignout.tblImg.FieldByName('Picture'), bmWrite);
try
J.SaveToStream(S);
finally
S.Free;
end;
finally
J.Free;
end;
Image1.Picture := nil;
Close;
end;
Man Im just tired...of course its not file stream its memory stream..jeez.
This is the correct code to save Timage as JPEG in MSSQL express (~50kb/image):
procedure TfrmCapture.Button3Click(Sender: TObject);
var
j:TJPEGImage;
S : TMemorystream;
begin
J := TJPEGImage.Create;
S := Tmemorystream.Create;
try
J.Assign(image1.Picture.graphic);
J.CompressionQuality :=80;
J.Compress;
J.SaveToStream(S);
S.Position :=0;
(frmSignout.tblImg.FieldByName('Picture') as TblobField).LoadFromStream(S);
finally
J.Free;
S.Free;
end;
image1.Picture :=nil;
close;
end;
I have been making an application at Delphi XE3. I am trying to display values from database to the TAdvStringGrid component placed on the form. I am using dataset to display results at TAdvSTringGRid (code is given below). All other values are displaying perfectly except Image in database. Where it is expected to show image, it is showing junk characters. How to display image perfectly from DataBase at TAdvStringGrid.
SQLConnection1: TSQLConnection;
SQLMonitor1: TSQLMonitor;
DataSource1: TDataSource;
ADOConnection1: TADOConnection;
ClientDataSet1: TClientDataSet;
AdvStringGrid1: TAdvStringGrid;
procedure Button1Click(Sender: TObject);
procedure ShowSelectResults(results: TDataSet; sg: TAdvSTringGrid);
procedure FormCreate(Sender: TObject);
procedure TForm2.FormCreate(Sender: TObject);
var
results: TDataSet;
begin
SQLConnection1.Params.Add('Database=E:\playdb.s3db');
try
SQLConnection1.Connected := true;
SQLMonitor1.Active := True;
SQLConnection1.Execute('Select * from plays', nil, results);
except
on E: EDatabaseError do
ShowMessage('Exception raised with message' + E.Message);
end;
ShowSelectResults(results, advstringgrid1);
end;
Call to ShowSelectResult below
procedure TForm2.ShowSelectResults(results: TDataSet; sg: TAdvStringGrid);
var
names: TStringList;
i,j,k, rc: Integer;
resultsfield: variant;
Field: TblobField;
Stream: TStream;
Jpg: TJPEGImage;
Picture: TPicture;
begin
if not results.IsEmpty then
//Prints Data in the TAdvStringGrid
results.First;
j := 1;
while not results.EOF do
begin
if (j>sg.rowcount) then
sg.rowcount := sg.rowcount + 1;
for i := 0 to results.fields.Count - 1 do
begin
if i=0 then
else if i = 4 then
//Here I want to display image from db
Field := TBlobField(results.FieldByName(names[i]).AsString);
Stream := results.CreateBlobStream(Field, bmRead);
sg.CreatePicture(i, j, true, ShrinkWithAspectRatio, 20, haCenter, vaAboveText).Picture
else
sg.cells[i,j] := results.FieldByName(names[i]).AsString;
end;
results.Next;
inc(j);
end;
end;
Problem is at the else if i=4 loop in the above code at sg.CreatePicture (format of the CreatePicture procedure is given below), where I want to display image in that particular column.
In manual of TAdvStringGrid they have mentioned following methods for picture display at grid cells
Grid.CreatePicture(2,3,True,Shrink,0,haLeft,vaTop).LoadFromFile(‘TST.JPG’);
procedure AddPicture(ACol,ARow: Integer;APicture:TPicture;transparent: Boolean; stretchmode:TStretchMode; padding: Integer; hal:TCellHalign; val:TCellValign);
function GetPicture(ACol,ARow: Integer): TPicture;
Grid.CreateFilePicture(2,3,True,Shrink,0,haLeft,vaTop).Filename := ‘TST.JPG’;
But there is no mention about how to use it with DataSet.I am messing with CreatePicture procedure of TAdvStringGRid, not getting it worked out with DataSet.
Latest Development
Finally I find out way with the help of some scholars like Bummi to save the JPEG image into memorystream and then display same.
My latest code is as follows
procedure TForm2.ShowSelectResults(results: TDataSet; sg: TAdvStringGrid);
var
names: TStringList;
Field: TblobField;
//Stream: TStream;
Stream: TMemoryStream;
//blobType := TBlobType;
Jpg: TJPEGImage;
Picture: TPicture;
Image: TImage;
Graphic: TGraphic;
Begin
//k := results.FieldCount;
//sg.Rowcount := rc;
results.First;
j := 1;
while not results.EOF do
begin
if (j>sg.rowcount) then
sg.rowcount := sg.rowcount + 1;
for i := 0 to results.fields.Count - 1 do
begin
if i=0 then
else if i = 4 then // Column 5 for Image
begin
try
if ((results.FieldByName(names[i]).AsString) <> '') then
Begin
Stream := TMemoryStream.Create;
Image := Timage.Create(Self);
Jpg := TJPEGImage.Create;
Picture := TPicture.Create;
Field := TBlobField(results.FieldByName('image'));
Stream := results.CreateBlobStream(Field, bmReadWrite);
//Field.SaveToStream(Stream);
Stream.Position := 0;
Jpg.LoadFromStream(Stream);
Picture.Assign(Jpg);
//Jpg.LoadFromFile('C:\Sample Pictures\Cabo.jpg');
//Picture.Assign(Jpg);
sg.AddPicture(i,j,Picture,True,ShrinkWithAspectRatio,0,haLeft,vaTop);
end;
finally
Jpg.Free;
Stream.Free;
end;
end
else
//Prints data in other columns
sg.cells[i.j] := results.FieldByName(names[i]).AsString;
inc(j);
end;
end;
Now it's facing some memory issue according to me at the line Jpg.LoadFromStream(Stream);
It is error code JPEG Error #53 , I came to know that above such error code display only when image you are trying to access via memorystream is corrupted but I have made sure image is not corrupted and displaying properly with the help of other software extracted from similar database. I also have renewed the image in the database. Still why I am getting JPEG Error #53. Problem is mainly at Jpg.LoadFromStream(Stream)
Note that the with commented code
Jpg.LoadFromFile('C:\Sample Pictures\Cabo.jpg');
Picture.Assign(Jpg);
sg.AddPicture(i,j,Picture,True,ShrinkWithAspectRatio,0,haLeft,vaTop);
When it is extracted from static file it works perfectly. Problem is only with the MemoryStream. How to rectify this error?
CreateBlobStream is creating a TStream object, not a TMemoryStream.
Since you do not want to write the JPG to the database you should use bmRead instead of bmReadWrite.
I am not used to SQLite, but you will have to make sure that you are using a suitable binary datetype (BLOB).
JPG := TJpegImage.Create;
Picture:= TPicture.Create;
try
st := results.CreateBlobStream(TBlobField(results.FieldByName('image')), bmRead);
try
JPG.LoadFromStream(st);
Picture.Assign(JPG);
sg.AddPicture(i,j,Picture,True,ShrinkWithAspectRatio,0,haLeft,vaTop);
finally
st.Free;
end;
finally
JPG.Free;
Picture.Free;
end;
To ensure that the stored image is really a JPG you should write the JPG for testing with something like:
var
ms: TMemoryStream;
begin
ads.Open;
ads.Append;
ms := TMemoryStream.Create;
try
Image1.Picture.Graphic.SaveToStream(ms); // make sure having loaded a JPG
ms.Position := 0;
TBlobField(ads.FieldByName('image')).LoadFromStream(ms);
finally
ms.Free;
end;
ads.Post;
end;
I realize this is a tad late, but I wanted to contribute. I quite simply did the following, having not to worry about the image format (.jpg, .png etc.). I simply create a TStream object, load the BLOB stream into it, and then I load the Image from the stream. Short and sweet, and it works great for me.
var
Stream : TStream;
begin
try
Stream := TStream.Create;
Stream := Dataset.CreateBlobStream(Dataset.FieldByName('SIGNATURE'), bmRead);
Stream.Position := 0;
lblPicSize.Caption := 'Picture is ' + IntToStr(Stream.Size) + ' Bytes';
if Stream.Size <= 0 then
pnlPic.Caption := '<No Signature>'
else
pnlPic.Caption := '';
try
imgSignature.Picture.LoadFromStream(Stream);
except
on E:Exception do
begin
ShowMessage(E.Message);
end;
end;
finally
Stream.Free;
end;
end;