gpt4 book ai didi

delphi - 无法压缩 JPEG 图像并将其显示在屏幕上

转载 作者:行者123 更新时间:2023-12-03 15:07:49 26 4
gpt4 key购买 nike

我尝试在imgQInput(这是一个TImage)中加载图像,将其分配给TJpegImage,压缩它(压缩因子5)并在imgQOutput(另一个TImage)中显示它。但这不起作用。 imgQOutput 中的图像与原始图像相同。由于压缩因素,它看起来应该非常像素化!然而,压缩是有效的,因为当我将 JPEG 保存到磁盘时,它非常小。

  JPG:= TJPEGImage.Create;
TRY
JPG.CompressionQuality:= trkQuality.Position;
JPG.Assign(imgQInput.Picture.Graphic);
CompressJpeg(JPG);
imgQOutput.Picture.Assign(JPG); <--------- something wrong here. the shown image is not the compressed image but the original one
FINALLY
FreeAndNil(JPG);
END;


function CompressJpeg(OutJPG: TJPEGImage): Integer;
VAR tmpQStream: TMemoryStream;
begin
tmpQStream:= TMemoryStream.Create;
TRY
OutJPG.Compress;
OutJPG.SaveToStream(tmpQStream);
OutJPG.SaveToFile('c:\CompTest.jpg'); <--------------- this works
Result:= tmpQStream.Size;
FINALLY
FreeAndNil(tmpQStream);
END;
end;

最佳答案

您根本没有使用压缩的 JPG。

CompressJpeg 更改如下:

function CompressJpeg(OutJPG: TJPEGImage): Integer;
VAR tmpQStream: TMemoryStream;
begin
tmpQStream:= TMemoryStream.Create;
TRY
OutJPG.Compress;
OutJPG.SaveToStream(tmpQStream);
OutJPG.SaveToFile('c:\CompTest.jpg'); // You can remove this line.
tmpQStream.Position := 0; //
OutJPG.LoadFromStream(tmpQStream); // Reload the jpeg stream to OutJPG
Result:= tmpQStream.Size;
FINALLY
FreeAndNil(tmpQStream);
END;
end;

关于delphi - 无法压缩 JPEG 图像并将其显示在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com