2011-11-30 200 views
1

因此,我有一个应用程序需要原始图像,获取新的新裁剪区域,然后将裁切后的版本作为新文件保存。它的主要缺点是完美。新图像平均比原始图像大4倍。在我的测试中,我有一张大小约为4.5MB的照片,裁剪后的版本(正确裁剪并显得很好)在磁盘上大约为21MB。代码如下:Bitmap.Clone创建的图像比原始图像大4倍输出大小

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); 
+2

什么是输入文件的图形格式? – leppie

+0

对不起,我应该发布这是一个JPG格式。 – Llaslo

回答

2

这听起来像你加载的图像是一些其他格式比BMP(例如PNG或JPG)。

使用Bitmap.Save另一重载指定的ImageFormat

+0

就是这样。感谢您的帮助! – Llaslo

0

看那Bitmap.Save overload,让你选择输出格式。 默认情况下,它是Bmp我猜,它没有压缩。

所以,你的情况使用

croppedImage.Save(croppedFileName, originalImage.RawFormat); 
+1

感谢您的输入!我很感激。 – Llaslo

相关问题