TPicture does not hold a TGraphic unless you tell it to. It starts out empty, so the Graphic property is null. That's why you get an access violation when you try to call methods on it.
If you don't already know what kind of graphic you've stored, you'll either have to write something that inspects the contents of the field to figure it out, or add another field to your database that describes the format of the graphic field.
Once you know what kind of graphic you have, you can create an instance of that class, load it from the stream, and assign it to the TPicture container. Free your graphic afterward since TPicture creates its 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 the known type is always one of the graphic properties that TPicture already has, then you can access the type-specific property directly and skip the step of allocating your own graphic object. For example, if your database holds bitmaps, then you can access TPicture.Bitmap instead of TPicture.Graphic. Then TPicture will create a TBitmap object automatically. For example:
MyImage.Picture.Bitmap.LoadFromStream(MyStream);
withstatement and replace it with an explaining variable. Which line does the access violation occur on? - Kenneth Cochran