私は画像をアップロードできるMVCアプリケーションを持っており、最大サイズにリサイズされます。 50KB 私はwhileループでサイズ変更を行いますが、ファイルサイズが大きくなる画像の幅と高さを小さくすると問題になります。特定の時点で、サイズは小さくなるが、品質C# - 画像の解像度を下げるとファイルサイズが大きくなる
Request.InputStream.Position = 0;
string Data = new System.IO.StreamReader(Request.InputStream).ReadToEnd();
var Base64 = Data.Split(',')[1];
var BitmapBytes = Convert.FromBase64String(Base64);
var Bmp = new Bitmap(new MemoryStream(BitmapBytes));
while (BitmapBytes.Length > 51200)
{
int Schritte = 20; //I tested here also with 300
int maxWidth = Bmp.Width;
maxWidth = maxWidth - Schritte;
int maxHeight = Bmp.Height;
maxHeight = maxHeight - Schritte;
Bmp = ScaleImage(Bmp, maxWidth, maxHeight);
var base64 = ReturnImageAsBase64(Bmp);
BitmapBytes = Convert.FromBase64String(base64);
}
の費用でコードサイズを変更する:
public static Bitmap ScaleImage(Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth/image.Width;
var ratioY = (double)maxHeight/image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
}
return newImage;
}
私は66964バイトのサイズで始まります。ループの最初のターンの後、幅は300ピクセル、高さは420ピクセルであったものの、85151バイトとなりました。
です。たとえば1bpp(黒または白)のBitmapを持ってScaleImageのnewImageに描画する場合、newImageはデフォルトのPixelformatで作成されます。これはColorsの可能性が高いため、24bppになります。その結果、メモリサイズがはるかに大きくなります。 –