将JPEG错误42存储在数据库中时,为什么会出现JPEG错误42? [英] Why do I get JPEG error 42 when it's stored in a database?

查看:695
本文介绍了将JPEG错误42存储在数据库中时,为什么会出现JPEG错误42?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Delphi从MySql数据库中的TImage中显示图片.

I have problem to display my Picture in TImage from my MySql database using Delphi.

我使用此代码将图片保存到数据库中,并且可以完美地工作.

I save picture to my database with this code and work Perfectly.

var
  AStream : TMemoryStream;

AStream := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(AStream);
    AStream.Position := 0;
    if ADODataSet1.Active then
    begin
      ADODataSet1.Edit;
      TBlobField(ADODataSet1.FieldByName('MyField')).LoadFromStream(AStream);
      ADODataSet1.Post;
    end;
  finally
    AStream.Free;
  end;

但是,问题是当我想检索该图片时.我使用此代码,但只能显示数据库中的第一张图像.我在DBGrid事件-OnDrawColumnCell上使用此代码.并且当我使用此代码时,我收到消息JPEG错误#42!

But, problem is when I want to retrieve that Pictures. I use this code but I can display only first image from my database. I uses this code on DBGrid event - OnDrawColumnCell. AND WHEN I USE THIS CODE I'M GETTING MESSAGE JPEG ERROR #42!

var
  AStream : TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    if ADODataSet1.Active then
    begin
      TBlobField(ADODataSet1.FieldByName('MyField')).SaveToStream(AStream);
      AStream.Position := 0;
      Image1.Picture.Graphic.LoadFromStream(AStream);
    end;
  finally
    AStream.Free;
  end;
end;

有人可以教我如何解决此问题. 谢谢.

Can somebody show me how to Fix This Problem. Thank you.

推荐答案

JPEG错误42.例如,如果您尝试将长度为零的文件加载到TJPEGImage中,则最终结果为错误42.

JPEG error 42 is reported when the stream is truncated. For example, if you attempt to load a zero length file into a TJPEGImage then error 42 is the end result.

如果显示的是某些图像,但不是全部,那么最可能的解释是数据以某种方式未实现往返数据库的往返.

If some images display, but not all, then the most likely explanation is that the data is somehow not making the round-trip to the DB and back.

在写入时检查BLOB字段的大小.将其写入磁盘文件时,请检查它是否与文件大小一致.检查磁盘文件是否为有效的JPEG.然后,当您重新读取时,确认BLOB字段具有完全相同的长度.也许您的数据库代码有问题,并且流被截断了.

Check the size of the BLOB field when you write it out. Check that it tallies with the size of the file when you write it to a disk file. Check that the disk file is a valid JPEG. Then confirm that the BLOB field has the exact same length when you re-read it. Perhaps there's something wrong with your DB code and the stream is getting truncated.

因此,这里的第一步是确认您可以恢复最初放入数据库中的完全相同的数据.

So, the very first step here is to confirm that you can recover the exact same data that you originally put into the DB.

我唯一想到的是图像控件中的图形并不总是JPEG.您用于加载图像的代码Image1.Picture.Graphic.LoadFromStream()假定数据为JPEG.如果您保存的不是JPEG,则LoadFromStream()将会失败.

The only other thought I have is that the graphic in the image control is not always a JPEG. The code that you use to load the image, Image1.Picture.Graphic.LoadFromStream() assumes that the data is a JPEG. If you had saved something other than a JPEG then LoadFromStream() would fail.

这篇关于将JPEG错误42存储在数据库中时,为什么会出现JPEG错误42?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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