0

Hello i'm using this function in order to resize/upload an image . There's any quick adjustment i could make in order to improve the resized image quality without changing the full function?

   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;
Teodor
  • 1,285
  • 4
  • 21
  • 37
  • Does your image contain a thumbnail image? If so, this might apply: "If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method." from [the documentation](http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx). – Daniel Hilgarth Sep 28 '11 at 09:59
  • possible duplicate of [Resizing an Image without losing any quality](http://stackoverflow.com/questions/87753/resizing-an-image-without-losing-any-quality) – Davide Piras Sep 28 '11 at 10:01
  • possible duplicate of [High Quality Image Scaling C#](http://stackoverflow.com/questions/249587/high-quality-image-scaling-c) – spender Sep 28 '11 at 10:01
  • Proper image resizing [is difficult, and has many pitfalls](http://nathanaeljones.com/163/20-image-resizing-pitfalls/). A correct implementation with jpeg support requires 5 pages of code. A correct implementation with GIF support requires 80 pages of code. I'd suggest [using a library to handle the resizing, cropping, and encoding properly](http://imageresizing.net). – Lilith River Jan 09 '12 at 21:29

4 Answers4

2

instead of this code:

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

use this one :

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);
nmokkary
  • 1,219
  • 3
  • 14
  • 24
0

use this snippet:

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));
}

stolen from this question/answer: Resizing an Image without losing any quality see Kris's ultra voted answer and vote it up :)

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • thank you I'm trying to implement your recommended function but i get System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. .. working on this i'm new to C# – Teodor Sep 28 '11 at 10:27
0

On my site, I use the following code to resize user images before they are sent to the page.

You may be able to make use of something similar - try investigating the various Interpolation modes.

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);
    }
}
Oliver
  • 11,297
  • 18
  • 71
  • 121
0

The best is not to use "GetThumbnailImage" this makes low quality. According to http://msdn.microsoft.com/de-de/library/system.drawing.image.getthumbnailimage%28VS.80%29.aspx

Resize the image by redrawing it like:

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;
}

found on

oberfreak
  • 1,799
  • 13
  • 20