Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void btnCrop_Click(object sender, EventArgs e)
        {
            string ImageName = Session["pic"].ToString();
            int x = Convert.ToInt32(X.Value);
            int y = Convert.ToInt32(Y.Value);
            int w = Convert.ToInt32(W.Value);
            int h = Convert.ToInt32(H.Value);
            double ratio = 0;
            //int w1 = int.Parse(imgCrop.Width.Value.ToString());
            double w1 = double.Parse(Session["width"].ToString());
            ratio = (w1 / 400.0);
            double w12 = w * ratio;
            double h12 = h * ratio;
            double x1 = x * ratio;
            double y1 = y * ratio;
            int wm = int.Parse(Math.Round(w12).ToString());
            int hm = int.Parse(Math.Round(h12).ToString());
            int xm = int.Parse(Math.Round(x1).ToString());
            int ym = int.Parse(Math.Round(y1).ToString());
            double imgratio = w12 / h12;
            string pathcrop = "";
            byte[] CropImage = Crop(path + Session["pic"].ToString(), w, h, x, y);
            using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
            {
                ms.Write(CropImage, 0, CropImage.Length);
                using (SD.Image CroppedImage = SD.Image.FromStream(ms, true))
                {
                    string SaveTo = path1 + Session["pic"].ToString();
                    CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
                    pnlCrop.Visible = false;
                    pnlCropped.Visible = true;
                    imgCropped.ImageUrl = "Admin/ProductImage/Product/Cropped/" + Session["pic"].ToString();
                    pathcrop = path1 + Session["pic"].ToString();
                    //Response.Redirect("crop.aspx?act=crop");
                    //ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('crop.aspx?act=crop');", true);
                }
            }
            try
            {
                Size thnmb = new Size();
                double height = (168.0 / imgratio);
                //thnmb.Height = 142;
                thnmb.Width = 168;
                thnmb.Height = int.Parse(Math.Round(height).ToString());
                double hbig = (392.0 / imgratio);
                Size big = new Size();
                big.Width = 392;
                // big.Height = 391;
                big.Height = int.Parse(Math.Round(hbig).ToString());
                Size zoom = new Size();
                zoom.Width = 600;
                zoom.Height = 600;
                string pathbig = "/Admin/ProductImage/Product/Big/" + Session["pic"].ToString();
                string paththumb = "/Admin/ProductImage/Product/Thumb/" + Session["pic"].ToString();
                string pathzoom = "/Admin/ProductImage/Product/Zoom/" + Session["pic"].ToString();
                string path5 = Request.PhysicalApplicationPath + "Admin\\ProductImage\\Product\\Cropped\\" + Session["pic"].ToString();
                string path4 = Request.PhysicalApplicationPath;

                //Common.resizeImage(System.Drawing.Image.FromFile(path5), big).Save(HttpContext.Current.Request.PhysicalApplicationPath + "Admin\\ProductImage\\Product\\Big\\" + Session["pic"].ToString());
                //Common.resizeImage(System.Drawing.Image.FromFile(path5), thnmb).Save(HttpContext.Current.Request.PhysicalApplicationPath + "Admin\\ProductImage\\Product\\Thumb\\" + Session["pic"].ToString());
                //Common.resizeImage(System.Drawing.Image.FromFile(path5), zoom).Save(HttpContext.Current.Request.PhysicalApplicationPath + "Admin\\ProductImage\\Product\\Zoom\\" + Session["pic"].ToString());
                Common.resizeImage(System.Drawing.Image.FromFile(path5), big).Save(path4 + "Admin\\ProductImage\\Product\\Big\\" + Session["pic"].ToString());
                Common.resizeImage(System.Drawing.Image.FromFile(path5), thnmb).Save(path4 + "Admin\\ProductImage\\Product\\Thumb\\" + Session["pic"].ToString());
                Common.resizeImage(System.Drawing.Image.FromFile(path5), zoom).Save(path4 + "Admin\\ProductImage\\Product\\Zoom\\" + Session["pic"].ToString());
                con.cmdmain.Parameters.AddWithValue("@imgthumb", paththumb);
                con.cmdmain.Parameters.AddWithValue("@imgbig", pathbig);
                con.cmdmain.Parameters.AddWithValue("@imgzoom", pathzoom);
                con.cmdnonquery("update product set pic=@imgthumb,pic_big=@imgbig,pic_zoom=@imgzoom where productid='" + Request["pid"] + "'");
                con.cmdmain.Parameters.AddWithValue("@pid", Request["pid"]);
                con.cmdnonquery("insert into product_images(pid,pic,picbig,piczoom) values(@pid,@imgthumb,@imgbig,@imgzoom)");
                pnlCrop.Visible = false;
                pnlCropped.Visible = true;
                imgCropped.ImageUrl = "Admin/ProductImage/Product/Cropped/" + Session["pic"].ToString();
                Response.Write("<script>alert('Item Cropped Successfully!!')</script>");
                Response.Redirect("ProductList.aspx");
            }
            catch (Exception ex)
            {
                Response.Write("some technical problem");
            }
            //lnkcrop.Visible = false;
           //lnkproceed.Visible = true;
        }


A generic error occurred in GDI+. this error rise during when image cropped and resize this image
...
please help me anybody
Posted
Updated 9-Feb-14 20:39pm
v3

1 solution

Hello Tripat,

pls refer here

C# a generic error occurred in GDI+ Solution[^]

All the best:)Write back if you need any help in the same
 
Share this answer
 
Comments
Tripat Bala Singh 10-Feb-14 2:58am    
Common.resizeImage(System.Drawing.Image.FromFile(path5), big).Save(path4 + "Admin\\ProductImage\\Product\\Big\\" + Session["pic"].ToString());
Common.resizeImage(System.Drawing.Image.FromFile(path5), thnmb).Save(path4 + "Admin\\ProductImage\\Product\\Thumb\\" + Session["pic"].ToString());
Common.resizeImage(System.Drawing.Image.FromFile(path5), zoom).Save(path4 + "Admin\\ProductImage\\Product\\Zoom\\" + Session["pic"].ToString());

"A generic error occurred in GDI+." this error rise on above code... your refer url not solve my problem
charles henington 10-Feb-14 3:07am    
Can you narrow down exactly when the error occurs? is it during the actual crop/resize or when saving the file? the reason I ask is if your saving the file but it's not an bmp, gif, jpg, or png then windows has no actual encoder/decoder for the image.
Tripat Bala Singh 10-Feb-14 3:24am    
static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
try
{
using (SD.Image OriginalImage = SD.Image.FromFile(Img))
{
using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;

Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);

MemoryStream ms = new MemoryStream();

bmp.Save(ms, OriginalImage.RawFormat);

return ms.GetBuffer();
}
}
}
}
catch (Exception Ex)
{
throw (Ex);
}
}
i have use above function after cropping my image then i resized my cropped image but that not perform and give "A generic error occurred in GDI+." this error
charles henington 8-May-14 7:30am    
what System.Drawing.Imaging.ImageFormat is the original image in? to save that image as i mentioned above it has to have an encoder for that image ex. Jpg,Bmp,Png, or gif

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900