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:
XML
<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $('#<%=imgToCrop.ClientID%>').Jcrop({
                onSelect: getAreaToCrop
            });
        });
        function getAreaToCrop(c) {
            $('#XCoordinate').val(parseInt(c.x));
            $('#YCoordinate').val(parseInt(c.y));
            $('#Width').val(parseInt(c.w));
            $('#Height').val(parseInt(c.h));
        }
    </script>



I am using this following code in button event:


C#
protected void btnCrop_Click(object sender, EventArgs e)
    {
        string croppedFileName = string.Empty;
        string croppedFilePath = string.Empty;
        string fileName = Path.GetFileName(imgToCrop.ImageUrl);
        string filePath = Path.Combine(Server.MapPath("~/uploads"), fileName);
        if (File.Exists(filePath))
        {

            System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath);
            Rectangle areaToCrop = new Rectangle(
                Convert.ToInt32(XCoordinate.Value),
                Convert.ToInt32(YCoordinate.Value),
                Convert.ToInt32(Width.Value),
                Convert.ToInt32(Height.Value));
            try
            {

                Bitmap bitMap = new Bitmap(areaToCrop.Width, areaToCrop.Height);
                using (Graphics g = Graphics.FromImage(bitMap))
                {
                    g.DrawImage(orgImg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), areaToCrop, GraphicsUnit.Pixel);
                }
                croppedFileName = "crop_" + fileName;
                croppedFilePath = Path.Combine(Server.MapPath("~/uploads"), croppedFileName);
                bitMap.Save(croppedFilePath);
                orgImg.Dispose();
                bitMap = null;
                File.Delete(filePath);

                pnlCrop.Visible = false;
                lblMsg.ForeColor = Color.Green;
                lblMsg.Text = "Image cropped and saved successfully";

                imgCropped.ImageUrl = "~/uploads/" + croppedFileName;
                btnReset.Visible = true;

                string avater_path = "uploads/" + croppedFileName;
                myProfile objPr_dal = new myProfile();
                string userId = Convert.ToString(Session["Userid"]);
                int result = objPr_dal.Set_user_avater(userId, avater_path);

                if (result == 1)
                {
                    Response.Redirect("profile.aspx");
                }

            }
            catch (Exception ex)
            {
                lblMsg.Text = "Oops!! error occured : " + ex.Message.ToString();
            }
            finally
            {
                fileName = string.Empty;
                filePath = string.Empty;
                croppedFileName = string.Empty;
                croppedFilePath = string.Empty;
            }
        }


when i click crop button error is occured/hit here,,
               Rectangle areaToCrop = new Rectangle(
                Convert.ToInt32(XCoordinate.Value),
                Convert.ToInt32(YCoordinate.Value),
                Convert.ToInt32(Width.Value),
                Convert.ToInt32(Height.Value));



Please give me solution as early as possible.......Thanks in advance
Posted
Updated 4-Mar-15 17:49pm
v3
Comments
[no name] 4-Mar-15 23:50pm    
Put a break point and check which area you get that error?
[no name] 4-Mar-15 23:53pm    
Check what do u get in XCoordinate.Value,YCoordinate.Value,Width.Value and Height.Value ?
There must be something non-integral value hence the error occured
Which line of code gives you this exception?
Litan Sarker 5-Mar-15 4:55am    
protected void btnCrop_Click(object sender, EventArgs e)
{
string croppedFileName = string.Empty;
string croppedFilePath = string.Empty;
string fileName = Path.GetFileName(imgToCrop.ImageUrl);
string filePath = Path.Combine(Server.MapPath("~/uploads"), fileName);
if (File.Exists(filePath))
{

System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath);
Rectangle areaToCrop = new Rectangle(
Convert.ToInt32(XCoordinate.Value),
Convert.ToInt32(YCoordinate.Value),
Convert.ToInt32(Width.Value),
Convert.ToInt32(Height.Value));
try
{
..............
.........
I am sending value by the following code
Rectangle areaToCrop = new Rectangle(
Convert.ToInt32(XCoordinate.Value),
Convert.ToInt32(YCoordinate.Value),
Convert.ToInt32(Width.Value),
Convert.ToInt32(Height.Value));
Litan Sarker 7-Mar-15 1:47am    
If i use this process in Form then it works well,but when i use it in content page it doesn't work,,How can i solve this problem?? please help me

1 solution

Please update code

jquery
$(document).ready(function () {
           $('#<%=imgToCrop.ClientID%>').Jcrop({
               onSelect: getAreaToCrop
           });
       });


Pass the value onselect :getAreaCrop(Passgeting value)


jquery
 onSelect: getAreaToCrop("Pass geting value here")

Your Error is the don't pass any value and You call function with Argument and here Function getAreaToCrop(c) in c Value null So this kind of error.

C#
function getAreaToCrop(c) {
           $('#XCoordinate').val(parseInt(c.x));
           $('#YCoordinate').val(parseInt(c.y));
           $('#Width').val(parseInt(c.w));
           $('#Height').val(parseInt(c.h));
       }
 
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