Note that there are some explanatory texts on larger screens.

plurals
  1. POWorking with images: Parameter is not valid
    text
    copied!<p>It all started with <a href="https://stackoverflow.com/questions/4161873/reduce-image-size-c-sharp/4161930#4161930">a very usefull piece of code, that I found here on Stackoverflow</a>.</p> <p>Then, I decided to make my own tweaks, and add image resizing to this method. However, after struggling with this problem I am now being presented with the information of: <strong>"The parameter is not valid"</strong>.</p> <p>I would also like to highlight that, despite the error, the images are being sucessfully uploaded. However, they are not being optmized as intended.</p> <hr> <p><strong>This is the part of the code in my "upload button":</strong></p> <pre><code>fuOne.SaveAs(Server.MapPath("~/imgFolder/temp/") + fuOne.FileName); System.Drawing.Image imgUploaded = System.Drawing.Image.FromFile(Server.MapPath("~/imgFolder/temp/") + fuOne.FileName); SaveJpeg(Server.MapPath("~/imgFolder/temp/") + fuOne.FileName, imgUploaded, 60, 300, 300); </code></pre> <hr> <p><strong>This is the full code of my SaveJpeg method:</strong></p> <pre><code>public static void SaveJpeg(string path, System.Drawing.Image imgUploaded, int quality, int maxWidth, int maxHeight) { if (quality &lt; 0 || quality &gt; 100) throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); // resize the image int newWidth = imgUploaded.Width; int newHeight = imgUploaded.Height; double aspectRatio = (double)imgUploaded.Width / (double)imgUploaded.Height; if (aspectRatio &lt;= 1 &amp;&amp; imgUploaded.Width &gt; maxWidth) { newWidth = maxWidth; newHeight = (int)Math.Round(newWidth / aspectRatio); } else if (aspectRatio &gt; 1 &amp;&amp; imgUploaded.Height &gt; maxHeight) { newHeight = maxHeight; newWidth = (int)Math.Round(newHeight * aspectRatio); } Bitmap newImage = new Bitmap(imgUploaded, newWidth, newHeight); Graphics g = Graphics.FromImage(imgUploaded); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; g.DrawImage(imgUploaded, 0, 0, newImage.Width, newImage.Height); g.Dispose(); imgUploaded.Dispose(); // Lets start to change the image quality EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); // Jpeg image codec ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; System.Drawing.Image imgFinal = (System.Drawing.Image)newImage; newImage.Dispose(); imgFinal.Save(path, jpegCodec, encoderParams); imgFinal.Dispose(); } /// &lt;summary&gt; /// Returns the image codec with the given mime type /// &lt;/summary&gt; private static ImageCodecInfo GetEncoderInfo(string mimeType) { // Get image codecs for all image formats ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); // Find the correct image codec for (int i = 0; i &lt; codecs.Length; i++) if (codecs[i].MimeType == mimeType) return codecs[i]; return null; } </code></pre> <hr> <h1>Follow up</h1> <p>It seems that the code had a couple of errors. Being one in the encoding and another in the image saving.</p> <p>The follow up of Aristos is very important to solving this problem, because it fixes my lousy mistake when saving the file. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload