2013-10-13 64 views
0

我正在使用從互聯網上獲得的方法,並對其進行了一些定製。上傳圖片時顏色發生變化

它需要一個HttpPostedFile從文件上傳和一些沒有必要的參數,然後調整圖片的大小和comperise它,然後將其保存在主機和返回位置

,但我上傳的圖片後,我發現它變得有點灰色,你可以在這裏看到兩張圖片的區別。

真實影像: enter image description here

上傳的圖片 Uploaded Image

如何解決,在我的方法。

我上傳方法

public string ResizeImage(HttpPostedFile PostedFile, string destinationfile, int maxWidth, int maxHeight) 
{ 

    float ratio; 

    // Create variable to hold the image 
    System.Drawing.Image thisImage = System.Drawing.Image.FromStream(PostedFile.InputStream); 

    // Get height and width of current image 
    int width = (int)thisImage.Width; 
    int height = (int)thisImage.Height; 

    // Ratio and conversion for new size 
    if (width < maxWidth) 
    { 
     ratio = (float)width/(float)maxWidth; 
     width = (int)(width/ratio); 
     height = (int)(height/ratio); 
    } 

    // Ratio and conversion for new size 
    if (height < maxHeight) 
    { 
     ratio = (float)height/(float)maxHeight; 
     height = (int)(height/ratio); 
     width = (int)(width/ratio); 
    } 

    // Create "blank" image for drawing new image 
    System.Drawing.Bitmap outImage = new System.Drawing.Bitmap(width, height); 
    System.Drawing.Graphics outGraphics = System.Drawing.Graphics.FromImage(outImage); 
    System.Drawing.SolidBrush sb = new System.Drawing.SolidBrush(System.Drawing.Color.White); 

    outGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
    outGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
    outGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 

    // Fill "blank" with new sized image 
    outGraphics.FillRectangle(sb, 0, 0, outImage.Width, outImage.Height); 
    outGraphics.DrawImage(thisImage, 0, 0, outImage.Width, outImage.Height); 
    sb.Dispose(); 
    outGraphics.Dispose(); 
    thisImage.Dispose(); 

    if (!destinationfile.EndsWith("/")) 
     destinationfile += "/"; 

    if (!System.IO.Directory.Exists(Server.MapPath(destinationfile))) 
     System.IO.Directory.CreateDirectory(Server.MapPath(destinationfile)); 

    // Save new image as jpg 
    string filename = Guid.NewGuid().ToString(); 
    outImage.Save(Server.MapPath(destinationfile + filename + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); 
    outImage.Dispose(); 

    return destinationfile + filename + ".jpg"; 
} 

編輯

我把打印屏幕,所以你可以看到兩個畫面

enter image description here

回答

1
之間的顏色差異

當涉及到顏色時,圖像之間的主要區別是原始圖像根本沒有顏色配置文件,而處理後的圖像具有sRGB顏色配置文件。

根據瀏覽器,操作系統以及您的屏幕如何校準,圖像將以略微不同的顏色顯示。對於沒有顏色配置文件的圖像,瀏覽器可以爲其設定一個顏色配置文件,也可以在沒有任何顏色校正的情況下顯示它。當我在我的電腦上查看帶有色彩校準屏幕的Firefox中的圖像時,實際上我看不到任何色差。

當您保存圖像時,JPEG編碼器採用了sRGB顏色配置文件,與原始圖像中根本沒有顏色配置文件信息時一樣,猜測與其他配置文件一樣。您需要上傳帶有顏色配置文件的圖像,以查看JPEG編碼器是否正確處理該圖像。只要沒有顏色配置文件,解釋圖像中的顏色值就沒有對或錯。

+0

我仍然可以看到Firefox中的區別:( –

+0

+1很好解釋。圖像看起來完全一樣,我的重要其他人注意到,也許OP一直盯着計算機太長:) – Zerkey

+0

LOL yea im在今天工作12小時以上的網站上工作,但你知道我問了我的一個家人,她說他們有區別。 :D 她說[真實圖像]是黃色的,[上傳的圖像]是白色的 –

相關問題