一句之差,LoadFromStream不行,LoadFromFile却可以,奇怪!(100分)

  • 主题发起人 主题发起人 hehuan
  • 开始时间 开始时间
H

hehuan

Unregistered / Unconfirmed
GUEST, unregistred user!
photoStream := TMemoryStream.create;
TBlobField(ADODataSet2.fieldbyname('act_photo')).SaveToStream(photoStream);
photoStream.SaveToFile('c:/photo.jpg');
jpgPhoto := TJpegImage.Create;
jpgPhoto.LoadFromFile('c:/photo.jpg');
photo.Picture.Assign(jpgPhoto);

但是如果把: jpgPhoto.LoadFromFile('c:/photo.jpg');
换成: jpgPhoto.LoadFromStream(photoStream);
其它语句顺序都不变,但就是不行,报错Jpeg #error 42 ,为什么会这样? 明明stream里有内容的,否则photoStream.SaveToFile('c:/photo.jpg');又怎么会成功呢?

请各位大大指教呀!谢谢~
 
我想可能是流對象聲明不符
 
那就试一下使用 TBlobStream (for working with BLOB fields)
TBlob stream works with persistent TBlobField objects (including descendants of TBlobField such as TGraphicField and TMemoField). BLOB fields use BLOB streams to read data from and write data to the dataset.

TBlobStream allows objects that have no specialized knowledge of how data is stored in a BLOB field to read or write such data by employing the uniform stream mechanism.
To use a BLOB stream, create an instance of TBlobStream, use the methods of the stream to read or write the data, and then free the BLOB stream. Do not use the same instance of TBlobStream to access data from more than one record. Instead, create a new TBlobStream object every time you need to read or write BLOB data on a new record.
 
photoStream := TMemoryStream.create;
TBlobField(ADODataSet2.fieldbyname('act_photo')).SaveToStream(photoStream);
photoStream.SaveToFile('c:/photo.jpg');
jpgPhoto := TJpegImage.Create;
photoStream.Position := 0;
jpgPhoto.LoadFromStream(photoStream);
photo.Picture.Assign(jpgPhoto);
 
就是少一个指定位置的, photoStream.Position := 0;
 
多人接受答案了。
 

Similar threads

后退
顶部