2015-05-14 55 views
0

加載代碼我的形象不是由文件系統壓縮

var memoryStream = new MemoryStream(); 
using (memoryStream) 
{ 
    System.Web.HttpContext.Current.Request.Files[upload].InputStream.CopyTo(memoryStream); 
    memoryStream.ToArray(); 
} 

byte[] bytes = memoryStream.GetBuffer();  

// Get the image from the server 
System.Drawing.Image image = new System.Drawing.Bitmap(System.Web.HttpContext.Current.Request.Files[upload].InputStream); 

調整圖像調用:

System.Drawing.Image image = this.ResizeImage(
    image, 
    originalImagePath, 
    ImageSizeType.Original, 
    null, 
    null) 

保存圖像調用:

image.Save(<location to save>); 

不壓縮圖像的代碼:

private System.Drawing.Image ResizeImage(System.Drawing.Image image, string filePath, string sizeType, int? _width, int? height) 
{ 
    ... 
    System.Drawing.Bitmap b = new System.Drawing.Bitmap(width, resizeHeight); 
    b.SetResolution(72, 72); 
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)b); 
    g.CompositingQuality = CompositingQuality.HighSpeed; 
    //g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    g.InterpolationMode = InterpolationMode.Low; 
    g.SmoothingMode = SmoothingMode.HighSpeed; 
    g.DrawImage(image, 0, 0, width, resizeHeight); 
    g.Dispose(); 
    return (System.Drawing.Image)b; 
} 

不管我做什麼,以這個形象,當它保存,這樣可以節省在真的很高。

例如...爲1024×768 @ 300KB JPG格式變成600×400 @ 800KB

我在做什麼錯?

+2

什麼是原始和調整大小的文件格式?您不顯示加載/保存代碼。 – GazTheDestroyer

+0

保存文件的格式是? – Magnus

+0

png,gif,jpg ...任何真的。無論用戶上傳 – Jimmyt1988

回答

1

由於馬格努斯說得很有道理,繪圖畫布,以使文件的大小......沒有什麼區別...

這是當時正在所有小白保存文件的一部分......這是它應該是:

private ImageCodecInfo GetEncoderInfo(string mimeType) 
    { 
     // Get image codecs for all image formats 
     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 

     // Find the correct image codec 
     for (int i = 0; i < codecs.Length; i++) 
      if (codecs[i].MimeType == mimeType) 
       return codecs[i]; 
     return null; 
    } 


... 
if(mimeType.ToLower() == "image/jpeg") 
{ 
    ImageCodecInfo jpgEncoder = this.GetEncoderInfo("image/jpeg") 

    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; 

    EncoderParameters myEncoderParameters = new EncoderParameters(1); 
    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 80L); 
    myEncoderParameters.Param[0] = myEncoderParameter; 

    image.Save(systemFilePath, jpgEncoder, myEncoderParameters); 
} 
else 
{ 
    image.Save(systemFilePath); 
}