英文:
Bitmap to JPEG Compression
问题
我一直在尝试将位图转换为压缩的 JPEG 以节省数据库空间,但目前还没有成功。我使用 Delphi 11.1 和 FMX。
我的代码如下:
var
NewBitmap: TBitmap;
CodecParams: TBitmapCodecSaveParams;
MS1: TMemoryStream;
Surf: TBitmapSurface;
JpgQuality: TBitmapCodecSaveParams;
begin
...
JpgQuality.Quality := 100;
MS1.Position := 0;
Surf := TBitmapSurface.Create;
try
Surf.Assign(NewBitmap);
// 使用编解码器将 Surface 保存到流中
if not TBitmapCodecManager.SaveToStream(
MS1,
Surf,
'.jpg') then
raise EBitmapSavingFailed.Create(
'保存位图为 jpg 时出错');
finally
Surf.Free;
end;
...
end;
请注意,我只翻译了代码部分,没有包括问题或其他内容。
英文:
I have been trying to convert bitmaps to compressed jpegs to save space in a database, but so far with no success. I use Delphi 11.1 with FMX.
My code looks as follows:
var
NewBitmap: TBitmap;
CodecParams : TBitmapCodecSaveParams;
MS1 : TMemoryStream;
Surf: TBitmapSurface;
JpgQuality : TBitmapCodecSaveParams;
begin
...
JpgQuality.Quality := 100;
MS1.Position := 0;
Surf := TBitmapSurface.create;
try
Surf.assign(NewBitmap);
// use the codec to save Surface to stream
if not TBitmapCodecManager.SaveToStream(
MS1,
Surf,
// '.jpg', JpgQuality) then // THIS DOES NOT WORK
'.jpg') then // THIS DOES WORK BUT NO COMPRESSION (FORMAT MAY NOT EVEN BE JPEG)
raise EBitmapSavingFailed.Create(
'Error saving Bitmap to jpg');
finally
Surf.Free;
end;
...
end;
答案1
得分: 2
你可以看到 ASaveParams
的类型是 PBitmapCodecSaveParams
:
PBitmapCodecSaveParams = ^TBitmapCodecSaveParams;
正如AmigoJack所提到的,你需要使用一个指针:
var
NewBitmap: TBitmap;
MS1: TMemoryStream;
Surf: TBitmapSurface;
JpgQuality: TBitmapCodecSaveParams;
begin
NewBitmap := TBitmap.CreateFromFile('input.bmp');
MS1 := TMemoryStream.Create;
Surf := TBitmapSurface.Create;
try
MS1.Position := 0;
Surf.Assign(NewBitmap);
JpgQuality.Quality := 100;
if not TBitmapCodecManager.SaveToStream(MS1, Surf, '.jpg', @JpgQuality) then
raise EBitmapSavingFailed.Create('Error saving Bitmap to jpg');
MS1.SaveToFile('output.jpg');
finally
NewBitmap.Free;
MS1.Free;
Surf.Free;
end;
end;
英文:
If you check the function :
class function SaveToStream(const AStream: TStream; const ABitmap: TBitmapSurface; const AExtension: string; const ASaveParams: PBitmapCodecSaveParams = nil): Boolean; overload;
You can see that ASaveParams
is type of PBitmapCodecSaveParams
:
PBitmapCodecSaveParams = ^TBitmapCodecSaveParams;
As mentionned by AmigoJack you need to use a pointer :
var
NewBitmap: TBitmap;
MS1 : TMemoryStream;
Surf: TBitmapSurface;
JpgQuality : TBitmapCodecSaveParams;
begin
NewBitmap := TBitmap.CreateFromFile('input.bmp');
MS1 := TMemoryStream.Create;
Surf := TBitmapSurface.create;
try
MS1.Position := 0;
Surf.Assign(NewBitmap);
JpgQuality.Quality := 100;
if not TBitmapCodecManager.SaveToStream(MS1, Surf, '.jpg', @JpgQuality) then
raise EBitmapSavingFailed.Create('Error saving Bitmap to jpg');
MS1.SaveToFile('ouput.jpg');
finally
NewBitmap.Free;
MS1.Free;
Surf.Free;
end;
end;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论