我上傳的MVC2 Web應用程序用戶的圖像。用戶可以上傳任意大小的圖像,例如10MB或更多。上傳的圖像存儲在數據庫AS IMAGE
數據類型中。但在獲得存儲在數據庫之前,我想將其大小減小到4MB。我該怎麼做呢?減少映像中的字節,而在數據庫中存儲MVC2 C#4.0
我已減小尺寸用下面的代碼圖像的字節數:
if (file.ContentLength > 0)
{
//Create byte Array with file len
var imgByte = new Byte[file.ContentLength];
//force the control to load data in array
file.InputStream.Read(imgByte, 0, file.ContentLength);
System.IO.MemoryStream newImageStream =
new System.IO.MemoryStream(imgByte, 0, imgByte.Length);
Image image = Image.FromStream(newImageStream, true);
Bitmap resized = new Bitmap(image, new Size(800,600));
System.IO.MemoryStream stream = new System.IO.MemoryStream();
resized.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
var imgBytes = new Byte[stream.Length];
customParentalHealthUser.ImageBytes = imgBytes;
stream.Close();
stream.Dispose();
resized.Dispose();
}
不過,雖然顯示圖像,圖像沒有顯示出來,即使減小尺寸變得存儲在DB中。我認爲在縮小尺寸的同時,圖像變得腐敗或者其他。
請推薦?
感謝
能否請您提供FOT的代碼?這裏是我的代碼:如果(請求!= NULL){ HttpPostedFileBase文件= Request.Files [ 「uploadimage」]; 如果(文件!= NULL) 如果(file.ContentLength> 0){ 變種 = imgByte新字節[file.ContentLength]; file.InputStream.Read(imgByte,0,file.ContentLength); customParentalHealthUser.ImageBytes = imgByte; } } – Tarun 2011-03-24 06:27:30
@Tarun,已經有關於[100個實施例](http://stackoverflow.com/search?q=%5Bc%23%5D+resize+image)的如何調整在c#圖像。 – 2011-03-24 06:32:42
但我需要減少圖像的字節大小,因爲我將圖像作爲字節數組存儲在數據庫中? – Tarun 2011-03-24 06:40:39