Delphi从CreateBlobStream ADO Query加载TImage [英] Delphi loading TImage from CreateBlobStream ADO Query

查看:705
本文介绍了Delphi从CreateBlobStream ADO Query加载TImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用下面的代码将存储在varbinary mssql列中的图像数据加载到TImage组件,但是它出现错误

I have been trying to load image data stored in a varbinary mssql column to a TImage component using the code below, but it gives an 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 is Access violation....

请有人协助如何这样做

推荐答案

TPicture 不持有 TGraphic ,除非你告诉它。它开始为空,因此 Graphic 属性为null。这就是为什么当您尝试调用方法时,您会收到访问冲突的原因。

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.

一旦你知道你有什么样的图形,你可以创建该类的实例,从流中加载它,并将其分配给 TPicture 容器。自 TPicture 创建自己的图形副本后,自由形成图形。这里有一个例子:

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;

如果已知类型始终是 TPicture 已经有,那么你可以直接访问类型特定的属性,并跳过分配你自己的图形对象的步骤。例如,如果您的数据库持有位图,则可以访问 TPicture.Bitmap 而不是 TPicture.Graphic 。然后 TPicture 将自动创建一个 TBitmap 对象。例如:

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);

这篇关于Delphi从CreateBlobStream ADO Query加载TImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆