私のプロジェクトでは、イメージのサイズを変更し、それをフォルダに保存する必要があります。私はどのような方法で画像を変えずに画像のサイズを変更できるかを知るために多くの質問を投稿しましたが、まだ最良の方法は見つけられません。変更するときに画像のデータを保持するにはどうすればよいですか?
画像のサイズを変更しようとしません。サイズ変更方法で画像の100%のサイズ。
リサイズ方法:
public Image reduce(Image sourceImage, string size)
{
//for testing, i want to use the size of the source image
//double percent = Convert.ToDouble(size)/100;
int width = (int)(sourceImage.Width); //sourceImage.Width * percent
int height = (int)(sourceImage.Height); //sourceImage.Height *percent
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(sourceImage, destRect, 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
使用:
//the code to get the image is omitted (in my testing, jpg format is fixed, however, other image formats are required)
//to test the size of original image
oImage.Save(Path.Combine(oImagepath), System.Drawing.Imaging.ImageFormat.Jpeg);
Image nImage = resizeClass.reduce(oImage,"100");
nImage .Save(Path.Combine(nImagepath), System.Drawing.Imaging.ImageFormat.Jpeg);
結果:
画像の最初の節約:ファイルサイズ:10721キロバイト
画像の第二の省:ファイルサイズ:4033キロバイト < =それは10721キロバイト
なければならない問題は、ユーザが、彼らが通過する場合に4メガバイトの画像を受信するために受け入れることができる方法であります90%〜100%を設定して10MBイメージを作成しますか?それはばかげているので、私はプログラムを書き換える必要があり:(
画像:
元:リサイズhttps://1drv.ms/i/s!AsdOBLg50clihVaoEQdj1wQidhdX
:ここhttps://1drv.ms/i/s!AsdOBLg50clihVV96AVKouVkI25o
それはおそらく、JPEG圧縮の結果です。なぜそれはあなたにそれほど気になるのですか?保存されたファイルの[圧縮レベルの設定](https://msdn.microsoft.com/en-us/library/bb882583%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396)を試してください。 – slawekwin