我正在使用從互聯網上獲得的方法,並對其進行了一些定製。上傳圖片時顏色發生變化
它需要一個HttpPostedFile從文件上傳和一些沒有必要的參數,然後調整圖片的大小和comperise它,然後將其保存在主機和返回位置
,但我上傳的圖片後,我發現它變得有點灰色,你可以在這裏看到兩張圖片的區別。
真實影像:
上傳的圖片
如何解決,在我的方法。
我上傳方法
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";
}
編輯
我把打印屏幕,所以你可以看到兩個畫面
我仍然可以看到Firefox中的區別:( –
+1很好解釋。圖像看起來完全一樣,我的重要其他人注意到,也許OP一直盯着計算機太長:) – Zerkey
LOL yea im在今天工作12小時以上的網站上工作,但你知道我問了我的一個家人,她說他們有區別。 :D 她說[真實圖像]是黃色的,[上傳的圖像]是白色的 –