這樣的事情,我使用MVC因此HttpPostedFileBase
。但是,這是輸入一個file
輸入類型並返回一個字節數組,非常適合上傳到數據庫。
using System.Drawing;
using System.Drawing.Drawing2D;
private static byte[] PrepImageForUpload(HttpPostedFileBase FileData)
{
using (Bitmap origImage = new Bitmap(FileData.InputStream))
{
int maxWidth = 165;
int newWidth = origImage.Width;
int newHeight = origImage.Height;
if (origImage.Width < newWidth) //Force to max width
{
newWidth = maxWidth;
newHeight = origImage.Height * maxWidth/origImage.Width;
}
using (Bitmap newImage = new Bitmap(newWidth, newHeight))
{
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(origImage, new Rectangle(0, 0, newWidth, newHeight));
MemoryStream ms = new MemoryStream();
newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
}
}
你能分享一下你到目前爲止試過的代碼嗎? – Cyberdrew 2011-03-14 15:47:13
使用http://imageresizing.net庫獲得最佳圖像質量和正確的內存管理。使用(MemoryStream ms = new MemoryStream()){ImageBuilder.Current.Build(postedFile,ms,new ResizeSettings(「maxwidth = 800&maxheight = 600&format = jpg」); return ms.ToArray();} – 2011-08-31 18:10:04