调整大小后的低质量图像

时间:2011-09-28 09:52:53

标签: c# image resize image-scaling

您好我正在使用此功能来调整/上传图像。我可以进行任何快速调整,以便在不改变全部功能的情况下提高调整后的图像质量?

   FileUpload FileUpload1 =(FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
                string virtualFolder = "~/albume/";
                string physicalFolder = Server.MapPath(virtualFolder);
                string fileName = Guid.NewGuid().ToString();
                string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
                FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
                //test resize
                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/albume/") + fileName + extension);
                int srcWidth = img.Width;
                int srcHeight = img.Height;
                int thumbHeight = (int)((800.0 / srcWidth) * srcHeight);
                System.Drawing.Image thumb = img.GetThumbnailImage(800, thumbHeight, null, IntPtr.Zero);
                img.Dispose();
                FileUpload1.Dispose();
                thumb.Save(Server.MapPath("~/albume/") + fileName + extension, System.Drawing.Imaging.ImageFormat.Jpeg);

                //end resize
                myAlbum.poza = fileName + extension;

4 个答案:

答案 0 :(得分:1)

代替此代码:

thumb.Save(Server.MapPath("~/albume/") + fileName + extension, System.Drawing.Imaging.ImageFormat.Jpeg);

使用这个:

System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.EncoderParameters param = new System.Drawing.Imaging.EncoderParameters(1);
param.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
thumb.Save(Server.MapPath("~/albume/") + fileName + extension, info[1], param);

答案 1 :(得分:0)

使用此代码段:

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(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}

从这个问题/答案中偷来了:Resizing an Image without losing any quality看到克里斯的超选票并将其投票:)

答案 2 :(得分:0)

在我的网站上,我使用以下代码在将用户图像发送到页面之前调整其大小。

您可以使用类似的东西 - 尝试调查各种插值模式。

using (Graphics graphics = Graphics.FromImage(target))
{
    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
    graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    graphics.Clear(ColorTranslator.FromHtml("#F4F6F5"));
    graphics.DrawImage(bmp, 0, 0, 145, 145);
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    {
        target.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);                    
        memoryStream.WriteTo(context.Response.OutputStream);
    }
}

答案 3 :(得分:0)

最好不要使用“GetThumbnailImage”这样会降低质量。根据{{​​3}}

通过重绘图像来调整图像大小:

private static Image resizeImage(Image imgToResize, Size size)
{
   int sourceWidth = imgToResize.Width;
   int sourceHeight = imgToResize.Height;

   float nPercent = 0;
   float nPercentW = 0;
   float nPercentH = 0;

   nPercentW = ((float)size.Width / (float)sourceWidth);
   nPercentH = ((float)size.Height / (float)sourceHeight);

   if (nPercentH < nPercentW)
      nPercent = nPercentH;
   else
      nPercent = nPercentW;

   int destWidth = (int)(sourceWidth * nPercent);
   int destHeight = (int)(sourceHeight * nPercent);

   Bitmap b = new Bitmap(destWidth, destHeight);
   Graphics g = Graphics.FromImage((Image)b);
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;

   g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
   g.Dispose();

   return (Image)b;
}

http://msdn.microsoft.com/de-de/library/system.drawing.image.getthumbnailimage%28VS.80%29.aspx