lundi 9 mai 2016

A generic error occurred in GDI+, Getting this exception in my code when i try to save optimized image on server

Here is my code below which i am using to redraw and save the image.

Bitmap bitmap = new Bitmap(HttpContext.Current.Server.MapPath(filePath + Path.GetFileName(fileName)));

                    int newWidth = 100;
                    int newHeight = 100;

                    int iwidth = bitmap.Width;
                    int iheight = bitmap.Height;

                    if (iwidth <= 100)
                        newWidth = iwidth;

                    if (iheight <= 100)
                        newHeight = iheight;

                    bitmap.Dispose();

                    // ONCE WE GOT ALL THE INFORMATION, WE'll NOW PROCESS IT.

                    // CREATE AN IMAGE OBJECT USING ORIGINAL WIDTH AND HEIGHT.
                    // ALSO DEFINE A PIXEL FORMAT (FOR RICH RGB COLOR).

                    System.Drawing.Image objOptImage = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);

                    // GET THE ORIGINAL IMAGE.
                    using (System.Drawing.Image objImg =
                        System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(filePath + fileName)))
                    {

                        // RE-DRAW THE IMAGE USING THE NEWLY OBTAINED PIXEL FORMAT.
                        using (System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(objOptImage))
                        {
                            var _1 = oGraphic;
                            System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);

                            _1.DrawImage(objImg, oRectangle);
                        }

On the below lines of code i am getting error while i try to execute this code after uploading image. If image size is around 10 kb it will not throw any error but if it exceeds then 10 kb then it will.

 SAVE THE OPTIMIZED IMAGE.
                    objOptImage.Save(HttpContext.Current.Server.MapPath(filePath + "TempSubImages/" + fileName),
                        System.Drawing.Imaging.ImageFormat.Png);**

                    objImg.Dispose();
                }

                objOptImage.Dispose();

                // FINALLY SHOW THE OPTIMIZED IMAGE DETAILS WITH SIZE.
                Bitmap bitmap_Opt = new Bitmap(HttpContext.Current.Server.MapPath(filePath + "TempSubImages/" + Path.GetFileName(fileName)));
                byte[] bytes = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(filePath + "TempSubImages/" + Path.GetFileName(fileName)));

                UI.BasePage.TemporaryFile = bytes;
                String[] ArrContentType = new String[] { postedFile.ContentType, fileName };
                UI.BasePage.TemporaryStrings = ArrContentType;

                int iwidth_Opt = bitmap_Opt.Width;
                int iheight_Opt = bitmap_Opt.Height;
                bitmap_Opt.Dispose();

Aucun commentaire:

Enregistrer un commentaire