我一直在尝试使用下面的代码将存储在 varbinary mssql 列中的图像数据加载到 TImage 组件,但出现错误
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;
错误是访问冲突......
请有人协助如何执行此操作
最佳答案
TPicture
不会保存 TGraphic
,除非您告诉它。它一开始是空的,因此 Graphic
属性为 null。这就是当您尝试调用其方法时会遇到访问冲突的原因。
如果您还不知道存储了哪种图形,则必须编写一些内容来检查字段的内容以找出答案,或者向数据库中添加另一个描述格式的字段图形字段。
一旦您知道自己拥有哪种图形,您就可以创建该类的实例,从流中加载它,并将其分配给 TPicture
容器。之后释放您的图形,因为 TPicture
创建了自己的图形副本。这是一个例子:
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;
如果已知类型始终是TPicture
已有的图形属性之一,那么您可以直接访问特定于类型的属性并跳过分配自己的图形对象的步骤。例如,如果您的数据库保存位图,则您可以访问 TPicture.Bitmap
而不是 TPicture.Graphic
。然后TPicture
会自动创建一个TBitmap
对象。例如:
MyImage.Picture.Bitmap.LoadFromStream(MyStream);
关于Delphi 从 CreateBlobStream ADO 查询加载 TImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9825513/