使用 Delphi 2010 从 BLOB 加载时出现 Jpeg 错误 #51,使用 Delphi 2006 则正常

发布于 2024-09-16 12:14:45 字数 1342 浏览 8 评论 0原文

我有一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

り繁华旳梦境 2024-09-23 12:14:46

在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.

时常饿 2024-09-23 12:14:46

您正在使用一种相当尴尬的方式将 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 ?

浅忆 2024-09-23 12:14:46

将 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.

小…红帽 2024-09-23 12:14:46

为什么还要越过弦呢?直接写入流怎么样?图像字段可能是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文