|
Question : [c#]Invalid parameter used when trying to save a graphic.
|
|
Hi,
edit: sorry, this is the wrong area. This should go to c# offcourse!
well, 1st of all, lets see if I can do this right :)
2nd: The problem!
I have an uploader class, wich (offcourse) uploads an image to the server and then it should resize it. I started of writing this in vb.Net (wich is my "native" language) but as I was studying c# aswell, i figured, let's rewrite it to c#. The vb.Net version worked great. But I've converted it to c# and now I get an error. (Invalid parameter used.)
Here's the code: This is the code to upload an image. [code] public string UploadImage(System.Web.HttpPostedFile inputinstance) { if(! (inputinstance == null)) { string strLongFilePath = inputinstance.FileName ; string filename; filename = System.Guid.NewGuid().ToString()+".jpg"; string serverpath = @ConfigurationSettings.AppSettings["storagefolder"].ToString(); inputinstance.SaveAs(serverpath + filename); string fullpath; fullpath = serverpath + filename; ResizeImage(fullpath); return bestandsnaam; } else throw new ArgumentException("Some error message :)"); } [/code]
Here's the code of ResizeImage: [code] private string ResizeImage(string ImgPath) { int width,height; int canvaswidth,canvasheight; System.Drawing.Imaging.ImageFormat imgFormat = GetImageFormat(ImgPath); System.Drawing.Image img = System.Drawing.Image.FromFile(ImgPath); if (img.Width <= 0 && img.Height <= 0) { throw new ArgumentException("X and / or Y cannot be neg. ints!"); } else { if (img.Height > img.Width) { canvaswidth = 1253; canvasheight = 1843; } else { canvaswidth = 1843; canvasheight = 1253; } }
int x ; int y ; x = img.Height; y = img.Width;
int newx, newy, offsetx, offsety ; if (x >= y) { if(1677 * y / x <= 1088) { newx = 1677 * y / x ; newy = 1677 ; } else { newx = 1088; newy = 1088 * x / y; }
} else { if(1677 * x / y <=1088) { newx = 1677 ; newy = 1677 * x / y; } else { newx = 1088 * y / x; newy = 1088; } }
width = newx; height = newy; offsetx = (canvaswidth - width) / 2; offsety = (canvasheight - height) / 2;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(canvaswidth, canvasheight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(((System.Drawing.Image)bmp)); g.FillRectangle(new System.Drawing.SolidBrush (System.Drawing.Color.White), 0, 0, canvaswidth, canvasheight); g.DrawImage(img, offsetx, offsety, width, height);
EncoderParameters eps = new EncoderParameters(1); eps.Param[0] = new EncoderParameter(Encoder.Quality, 90); ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
img.Dispose(); bmp.Save(@ImgPath,ici,eps); bmp.Dispose();
return null ; } [/code] Well, I hope this is enough for someone to help me. There are two more functions being used: GetImageForm and GetEncoderInfo, but as far as I know these are standard functions being used by a lot of people.
There is an error on the line: bmp.Save(@ImgPath,ici,eps); It's almost the last line of the code.
Please note: this is my first time asking a question here, so I hope I'm doing this correct.
Thanks in advance...
Ton
|
|
Answer : [c#]Invalid parameter used when trying to save a graphic.
|
|
PAQ, refund 200 points.
Thanks! amp ee admin
(Thanks for all your help today, TAD!)
|
|
|
|