אני משתמש בזה : public static byte ResizeImageFile(byte imageFile,int select) { int width=200, hight=200; switch(select) { case 1:{ width = 200; hight = 200;}break; case 2: { width = 410; hight = 200; } break; case 3: { width = 410; hight = 410; } break; } using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile))) { Size newSize = CalculateDimensions(oldImage.Size, width,hight); using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)) { using (Graphics canvas = Graphics.FromImage(newImage)) { canvas.SmoothingMode = SmoothingMode.AntiAlias; canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize)); MemoryStream m = new MemoryStream(); newImage.Save(m, ImageFormat.Jpeg); return m.GetBuffer(); } } } } public static Size CalculateDimensions(Size oldSize, int width, int hight) { Size newSize = new Size(); if (oldSize.Height > oldSize.Width) { newSize.Width = width; newSize.Height = hight; } else { newSize.Width = width; newSize.Height = hight; } return newSize; }
|