位图.克隆创建的图像输出大小比原始图像大4倍

本文关键字:图像 原始 4倍 输出 创建 位图 | 更新日期: 2023-09-27 18:16:39

所以,我有一个应用程序,获取原始图像,获得新的新裁剪区域,然后将图像的裁剪版本保存为新文件。它工作得很完美,但有一个主要缺点。新图像平均比原始图像大4倍。在我的测试中,我有一张照片在磁盘上的大小约为4.5MB,而裁切版(裁切得当且看起来不错)在磁盘上的大小约为21MB。代码如下:

var originalImage = new Bitmap(imagePath);
var fWidth = originalImage.PhysicalDimension.Width;
var fHeight = originalImage.PhysicalDimension.Height;   
float calculatedWidth = GetCroppedWidth();
float calculatedHeight = GetCroppedHeight();
//Draw the image by centering the cropped region on the original
var heightOffset = (fHeight - calculatedHeight) / 2;
var widthOffset = (fWidth - calculatedWidth) / 2;
var sourceRectF = new RectangleF(widthOffset, heightOffset, calculatedWidth, calculatedHeight);
var croppedImage = originalImage.Clone(sourceRectF, originalImage.PixelFormat);
//Save the image
croppedImage.Save(croppedFileName);

位图.克隆创建的图像输出大小比原始图像大4倍

听起来你正在加载的图像是BMP以外的其他格式(例如PNG或JPG)。

使用指定ImageFormatBitmap.Save的另一个重载

看位图。保存重载,允许您选择输出格式。默认为Bmp, i guess,没有压缩。

所以,在你的例子中使用

croppedImage.Save(croppedFileName, originalImage.RawFormat);