使用 Delphi 2010 从 BLOB 加载时出现 Jpeg 错误 #51,使用 Delphi 2006 则正常
我有一个在 Delphi 2006 中开发的旧应用程序,现在需要对其进行一些更改。
在这个应用程序中,我从 MsSQL“图像”字段加载图像 但是当我用 delphi 2010 编译时出现错误: “引发了异常类 EJPEG,并带有消息“JPEG 错误 #51”。”
从数据库获取图像的代码:
aStream := TMemoryStream.Create;
Try
If LoadFromBlob(FieldByName('Picture'), aStream) then
begin
Pic:=TJpegImage.Create;
try
Try
Pic.LoadFromStream(aStream);
Picture.Assign(Pic); // <------ JPEG Error #51 here w D2010
Except
//something went wrong loading
HandleImageError();
End;
finally
Pic.Free;
end;
end;
Finally
aStream.Free;
End;
// ............
function LoadFromBlob(const AField: TField; const Stream: TStream): boolean;
var
ResultStr: string;
PResultStr: PChar;
begin
Result := false;
if (Assigned(AField)) and (Assigned(Stream)) then begin
try
ResultStr := AField.Value;
If ResultStr <> '' then
begin
PResultStr := PChar(ResultStr);
Stream.Write(PResultStr^, Length(ResultStr));
Stream.Seek(0,0);
Result := true;
end;
except
end;
end;
end;
用 Google 搜索了一下,发现错误 #51 的意思是:JERR_NO_QUANT_TABLE,无论这意味着什么。
当我用 Delphi 2006 编译时,相同的代码运行良好,没有错误,那么 D2010 出了什么问题呢?
I have an old application developed in Delphi 2006 that I now needed to make some changes in.
In this app, I'm loading images from a MsSQL "image" field
but when i compile with delphi 2010 I get the error :
"raised exception class EJPEG with message 'JPEG error #51'."
The code that get's the image from the database:
aStream := TMemoryStream.Create;
Try
If LoadFromBlob(FieldByName('Picture'), aStream) then
begin
Pic:=TJpegImage.Create;
try
Try
Pic.LoadFromStream(aStream);
Picture.Assign(Pic); // <------ JPEG Error #51 here w D2010
Except
//something went wrong loading
HandleImageError();
End;
finally
Pic.Free;
end;
end;
Finally
aStream.Free;
End;
// ............
function LoadFromBlob(const AField: TField; const Stream: TStream): boolean;
var
ResultStr: string;
PResultStr: PChar;
begin
Result := false;
if (Assigned(AField)) and (Assigned(Stream)) then begin
try
ResultStr := AField.Value;
If ResultStr <> '' then
begin
PResultStr := PChar(ResultStr);
Stream.Write(PResultStr^, Length(ResultStr));
Stream.Seek(0,0);
Result := true;
end;
except
end;
end;
end;
Googled around a bit and found out that error #51 means: JERR_NO_QUANT_TABLE, whatever that means.
When I compile with Delphi 2006, the same code works fine with no errors, so what's going wrong with D2010?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在D2010中,Char从Ansi变为Unicode,这意味着它占用两个字节而不是一个字节。
将 LoadFromBlob 中的变量更改为 AnsiString 和 PAnsiChar,它应该可以工作。
更新:您应该更好地访问 Field.AsAnsiString,而不是 Field.Value (Variant);变体访问可能包括隐式 Unicode->Ansi 转换,这会引入错误。
In D2010 Char changed from Ansi to Unicode, which means it occupies two bytes instead of one.
Change your variables in LoadFromBlob to AnsiString and PAnsiChar and it should work.
Update: Instead of Field.Value (Variant) you should better access Field.AsAnsiString; The variant access might include an implicit Unicode->Ansi conversion which introduces the error.
您正在使用一种相当尴尬的方式将 blob 字段保存到流中。为什么不使用 TBlobField.SaveToStream ?
You are using a rather awkward way to save a blob field to a stream. Why are you not using TBlobField.SaveToStream ?
将 aStream 保存到文件中并使用十六进制编辑器进行检查。比较 D2006 和 D2010 结果。
您在那里发现的内容应该会让您相信这与 JPEG 标头无关,并且很可能是 Unicode/Widechar 问题。
Save your aStream out to a file and examine with a hex editor. Compare the D2006 and D2010 results.
What you find there should convince you that it's nothing to do with the JPEG header, and it's likely a Unicode/Widechar issue.
为什么还要越过弦呢?直接写入流怎么样?图像字段可能是 TBlobField 或 TGraphicField。它们有一个 SaveToStream 方法,可以完全满足您的需求。
Why going the way over the strings anyway? What about directly writing to the stream? The image field probably is a TBlobField or TGraphicField. Those have a SaveToStream method that would perfectly suit your needs.