Delphi loads TImage from CreateBlobStream ADO Query
I am trying to load image data stored in varbinary mssql column into TImage component using below code but it gives error
with Query_Refresh do Begin close; open; if RecordCount> 0 then Begin // Edit to allow the streaming of the fields Edit; // MyStream is of Type TStream MyStream: = CreateBlobStream (FieldByName ('MyBlobField'), bmWrite); // Loading to the image --- Error occurs on the line below MyImage.Picture.Graphic.LoadFromStream (MyStream); End; End;
Error - Access Violation ....
Please help how to do this
source to share
TPicture
does not contain TGraphic
if you do not report it. It starts out empty, so the property Graphic
is null. This is why you get an access violation when you try to call methods on it.
If you don't already know what graphics you have saved, you will either have to write something that checks the contents of the field to figure it out, or add another field to your database that describes the format of the graphics field.
Once you know what kind of graphics you have, you can instantiate this class, load it from the stream, and assign it to the container TPicture
. Free your graphics after TPicture
creating your own copy of the graphic. Here's an example:
var
DBGraphicClass: TGraphicClass;
Graphic: TGraphic;
// Implementing FieldValToGraphicClass is an exercise for the reader.
DBGraphicClass := FieldValToGraphicClass(FieldByName('MyBlobFieldType'));
Graphic := DBGraphicClass.Create;
try
Graphic.LoadFromStream(MyStream);
MyImage.Picture.Graphic := Graphic;
finally
Graphic.Free;
end;
If a known type is always one of the graphics properties that it already has TPicture
, then you can directly access the type property and skip the step of selecting your own graphics. For example, if your database contains bitmaps, you can access TPicture.Bitmap
instead TPicture.Graphic
. Then TPicture
it will automatically create the object TBitmap
. For example:
MyImage.Picture.Bitmap.LoadFromStream(MyStream);
source to share