title: C# change dpi of an uploaded image
body: <p>I've got to following function which is called to change the resolution of an image. I want to do this so uploaded image with for example 300dpi will be modified to 72dpi (for web). This question is related to <a href="https://stackoverflow.com/questions/4737220/c-mvc-image-upload-resizing-server-side">another question</a> here on SO where i'm working on.</p>
  
  <p>I'm creation an extension method for this to be able to use this function on more places in my application, instead of only when uploading new files. (See above mentioned question)</p>
  
  <pre><code>public static byte[] SetDpiTo72(this byte[] imageToFit, string mimeType, Size newSize)
  {    
      using (MemoryStream memoryStream = new MemoryStream(), newMemoryStream = new MemoryStream())
      {
          memoryStream.Write(imageToFit, 0, imageToFit.Length);
          var originalImage = new Bitmap(memoryStream);
  
          using (var canvas = Graphics.FromImage(originalImage))
          {
              canvas.SmoothingMode = SmoothingMode.AntiAlias;
              canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
              canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
              canvas.DrawImage((Image)originalImage,0,0, newSize.Width, newSize.Height);
  
              newBitmap.SetResolution(72, 72);
              newBitmap.Save(newMemoryStream, ImageFunctions.GetEncoderInfo(mimeType), null);
          }
          return newMemoryStream.ToArray();
      }
  }
  </code></pre>
  
  <p>The mentioned extension methode is being called in a function similar to the situation below;</p>
  
  <pre><code>if (newSize.Width &gt; originalImage.Width &amp;&amp; newSize.Height &gt; originalImage.Height)
  {
       newSize.Width = originalImage.Width;
       newSize.Height = originalImage.Height;
  
       uploadedFileBuffer = uploadedFileBuffer.SetDpiTo72(uploadedFile.ContentType, newSize);
  
       return CreateFile(newSize, uploadedFile, uploadedFileBuffer);
  }
  </code></pre>
  
  <p>The bytearray coming in is the file as an bytearray. It already has the correct size, but I want to change the resolution to 72dpi. However after exectution and saving the image the resolution is still the originale entered resolution, which is 300dpi. How can I do this?</p>
  
  <p>UPDATE AFTER SEVERAL ANSWERS:</p>
  
  <pre><code>public static byte[] SetDpiTo72(this byte[] imageToFit, string mimeType, Size newSize)
          {
              using (MemoryStream memoryStream = new MemoryStream(), newMemoryStream = new MemoryStream())
              {
                  memoryStream.Write(imageToFit, 0, imageToFit.Length);
                  var originalImage = new Bitmap(memoryStream);
  
                  using (var canvas = Graphics.FromImage(originalImage))
                  {
                      canvas.SmoothingMode = SmoothingMode.AntiAlias;
                      canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                      canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                      canvas.DrawImage((Image)originalImage,0,0, newSize.Width, newSize.Height);
  
                      originalImage.SetResolution(72, 72);
  
                      var epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75);
                      var epParameters = new EncoderParameters(1);
                      epParameters.Param[0] = epQuality;
  
                      Image newimg = Image.FromStream(memoryStream);
  
                      //Getting an GDI+ exception after the execution of this line.
                      newimg.Save("C:\\test1234.jpg", ImageFunctions.GetEncoderInfo(mimeType), epParameters);
  
                      originalImage.Save("test.jpg", ImageFormat.Jpeg);
  
                      //This line give me an Argumentexception - Parameter is not valid.
                      //originalImage.Save(newMemoryStream, ImageFunctions.GetEncoderInfo(mimeType), epParameters);
                      //newMemoryStream.Close();
                  }
                  return newMemoryStream.ToArray();
              }
          }
  </code></pre>
  
  <p>The stackstrace which comes with the exception is telling me the following;</p>
  
  <pre><code>   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
     at Extensions.ByteArrayExtensions.SetDpiTo72(Byte[] imageToFit, String mimeType, Size newSize) in C:\Website\Project\Extensions\ByteArrayExtensions.cs:line 356
     at CMS.Presentation.FileFunctions.CreateFullsizeImage(HttpPostedFileBase uploadedFile, Size newSize, Byte[] uploadedFileBuffer) in C:\Website\Project\CMS.Presentation\FileFunctions.cs:line 197
     at CMS.Presentation.FileFunctions.CreateFile(HttpPostedFileBase uploadedFile, INodeService nodeservice, Guid userId, Node parentNode) in C:\Website\Project\CMS.Presentation\FileFunctions.cs:line 53
  </code></pre>
  
  <p>In the mean time I've also developed another function (see below) resizing just a bitmap. And this seem to work correctly. I can't use this function with my current implementation though because it returns just an Bitmap. Or should i change everything to work with bitmaps?</p>
  
  <pre><code>private static Bitmap ResizeImage(Image image, int width, int height)
          {
              var frameCount = image.GetFrameCount(new FrameDimension(image.FrameDimensionsList[0]));
              var newDimensions = ImageFunctions.GenerateImageDimensions(image.Width, image.Height, width, height);
              Bitmap resizedImage;
  
              if (frameCount &gt; 1)
              {
                  //we have a animated GIF
                  resizedImage = ResizeAnimatedGifImage(image, width, height);
              }
              else
              {
                  resizedImage = (Bitmap)image.GetThumbnailImage(newDimensions.Width, newDimensions.Height, null, IntPtr.Zero);
              }
  
              resizedImage.SetResolution(72,72);
  
              return resizedImage;
          }
  </code></pre>