Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project i need to crop the image file and i need to show the cropped image in another image control.
I tried this following code:

C#
using System;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace TestAppln
{
    public partial class crop : System.Web.UI.Page
    {
        protected void btnCrop_Click(object sender, EventArgs e)
        {
            int X1 = Convert.ToInt32(Request.Form["x1"]);
            int Y1 = Convert.ToInt32(Request["y1"]);
            int X2 = Convert.ToInt32(Request.Form["x2"]);
            int Y2 = Convert.ToInt32(Request.Form["y2"]);
            int X = System.Math.Min(X1, X2);
            int Y = System.Math.Min(Y1, Y2);
            int w = Convert.ToInt32(Request.Form["w"]);
            int h = Convert.ToInt32(Request.Form["h"]);

            
            string originalFile = Server.MapPath("~/images/miautito.jpg");


            using (Image img = Image.FromFile(originalFile))
            {
                using (System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(w, h))
                {
                    _bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                    using (Graphics _graphic = Graphics.FromImage(_bitmap))
                    {
                        _graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        _graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        _graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        _graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        _graphic.DrawImage(img, 0, 0, w, h);
                        _graphic.DrawImage(img, new Rectangle(0, 0, w, h), X, Y, w, h, GraphicsUnit.Pixel);

                        string extension = Path.GetExtension(originalFile);
                        string croppedFileName = Guid.NewGuid().ToString();
                        string path = Server.MapPath("~/cropped/");


                       
                        if (extension.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
                        {
                            extension = ".png";
                        }

                        string newFullPathName = string.Concat(path, croppedFileName, extension);

                        using (EncoderParameters encoderParameters = new EncoderParameters(1))
                        {
                            encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                            _bitmap.Save(newFullPathName, GetImageCodec(extension), encoderParameters);
                        }

                        lblCroppedImag.Text = string.Format("<img src='cropped/{0}' alt='Cropped image'>", croppedFileName + extension);
                    }
                }
            }
        }


        
        public static ImageCodecInfo GetImageCodec(string extension)
        {
            extension = extension.ToUpperInvariant();
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FilenameExtension.Contains(extension))
                {
                    return codec;
                }
            }
            return codecs[1];
        }
    }
}


I referred all the .js file.

Error:
A generic error occurred in GDI+.

Please help me .Thanks in advance
Posted
Comments
Sandeep Mewara 15-Sep-12 6:53am    
That error looks to 'generic' to comment anything. Anything else you see?
Dain Ucak 15-Sep-12 7:13am    
did you update your files write permission
P.Vinoth 17-Sep-12 6:24am    
I am getting error at this line
<pre_bitmap.Save(newFullPathName, GetImageCodec(extension), encoderParameters);></pre>

1 solution

 
Share this answer
 

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