我有然而這需要作物的圖像的部位上的操作,將得到的,裁剪圖像出來在文件大小方面顯著較大(原來是24K和裁剪後的圖像是像650K)。所以我發現我需要在保存之前對圖像應用一些壓縮。我想出了以下幾點:圖像壓縮是不工作
public static System.Drawing.Image CropImage(System.Drawing.Image image, Rectangle cropRectangle, ImageFormat format)
{
var croppedImage = new Bitmap(cropRectangle.Width, cropRectangle.Height);
using (var g = Graphics.FromImage(croppedImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(
image,
new Rectangle(new Point(0,0), new Size(cropRectangle.Width, cropRectangle.Height)),
cropRectangle,
GraphicsUnit.Pixel);
return CompressImage(croppedImage, format);
}
}
public static System.Drawing.Image CompressImage(System.Drawing.Image image, ImageFormat imageFormat)
{
var bmp = new Bitmap(image);
var codecInfo = EncoderFactory.GetEncoderInfo(imageFormat);
var encoder = System.Drawing.Imaging.Encoder.Quality;
var parameters = new EncoderParameters(1);
var parameter = new EncoderParameter(encoder, 10L);
parameters.Param[0] = parameter;
using (var ms = new MemoryStream())
{
bmp.Save(ms, codecInfo, parameters);
var resultImage = System.Drawing.Image.FromStream(ms);
return resultImage;
}
}
我設置質量低,看看是否有任何改變。沒有。作物被正確地保存在外觀上,但壓縮並不是一種快樂。如果我完全繞過CompressImage(),則文件大小和圖像質量都不會有任何不同。
所以,2個問題。爲什麼沒有發生?有沒有一種更簡單的方法來壓縮生成的圖像以進行「網頁優化」,類似於photoshop保存網頁圖像的方式(我認爲它只是從中減少了大量信息以減小尺寸)。
什麼圖像格式你在讀取和保存? BMP幾乎不壓縮。 PNG或JPG壓縮好得多。我可以很容易地從一個24k的PNG想象成一個650k的BMP。 –
我正在用png/jpg閱讀。位圖只是一箇中間對象 – Sinaesthetic
問題不在於你正在閱讀什麼......你在保存什麼。 Plaese檢查condecInfo和參數對象的正確設置...也許你保存圖像的質量比原來的更高... – HiperiX