Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UCrop.aspx.cs" Inherits="WebCrop.UCrop" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title></title>
    <link href="css/jquery.Jcrop.css" rel="stylesheet" />
    <script src="scripts/jquery.Jcrop.pack.js"></script>
    <script src="scripts/jquery.min.js"></script>
    <script src="scripts/jquery.Jcrop.js"></script>
    <script src="scripts/jquery.color.js"></script>
    <script src="scripts/jquery.Jcrop.min.js"></script>
    <link href="css/jquery.Jcrop.min.css" rel="stylesheet" />
    <script>
        $(document).ready(function()
        {

            $("#imgCrop").Jcrop({
                onSelect: storeCoords
            });
        });

        function storeCoords(c) {
            $("#X").val(c.x);
            $("#Y").val(c.y);
            $("#W").val(c.w);
            $("#H").val(c.h);

        };

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Panel ID="pnlUpload" runat="server">
      <asp:FileUpload ID="Upload" runat="server" />
      <br />
      <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
      <asp:Label ID="lblError" runat="server" Visible="false" />
    </asp:Panel>
    <asp:Panel ID="pnlCrop" runat="server" Visible="false">
      <asp:Image ID="imgCrop" runat="server" Height="258px" Width="285px" />
         <asp:Button ID="Button1" runat="server" Text="Crop" OnClick="btnCrop_Click" />
      <asp:HiddenField ID="X" runat="server" Value="0"/>
      <asp:HiddenField ID="Y" runat="server" Value="0"/>
      <asp:HiddenField ID="W" runat="server" Value="0"/>
      <asp:HiddenField ID="H" runat="server" Value="0"/>
    </asp:Panel>
    <asp:Panel ID="pnlCropped" runat="server" Visible="false">
      <asp:Image ID="imgCropped" runat="server" />
    </asp:Panel>
    </div>
    </form>
</body>
</html>

Codebehind File
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using SD = System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WebCrop
{
    public partial class UCrop : System.Web.UI.Page
    {
        String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
             Boolean FileOK = false;
             Boolean FileSaved = false;
  if (Upload.HasFile)
  {
    Session["WorkingImage"] = Upload.FileName;
    String FileExtension = Path.GetExtension(Session["WorkingImage"].ToString()).ToLower();
    String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
    for (int i = 0; i < allowedExtensions.Length; i++)
    {
      if (FileExtension == allowedExtensions[i])
      {
        FileOK = true;
      }
    }
  }

  if (FileOK)
  {
    try
    {
      Upload.PostedFile.SaveAs(path + Session["WorkingImage"]);
      FileSaved = true;
    }
    catch (Exception ex)
    {
      lblError.Text = "File could not be uploaded." + ex.Message.ToString();
      lblError.Visible = true;
      FileSaved = false;
    }
  }
       else
      {
    lblError.Text = "Cannot accept files of this type.";
    lblError.Visible = true;
       }

       if (FileSaved)
       {
    pnlUpload.Visible = false;
    pnlCrop.Visible = true;
    imgCrop.ImageUrl = "images/" + Session["WorkingImage"].ToString();
      }
}
protected void btnCrop_Click(object sender, EventArgs e)
        {
            string ImageName = Session["WorkingImage"].ToString();
            int h1 = Convert.ToInt32(X.Value);
            int h2 = Convert.ToInt32(Y.Value);
            int h3 = Convert.ToInt32(W.Value);
            int h4 = Convert.ToInt32(H.Value);

                byte[] CropImage = Crop(path + ImageName, h1, h2, h3, h4);
                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 = path + "crop" + ImageName;
                        CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
                        pnlCrop.Visible = false;
                        pnlCropped.Visible = true;
                        imgCropped.ImageUrl = "crop" + ImageName;
                    }
                }
            }

        static byte[] Crop(string Img, int X1, int Y1,int X2, int Y2 )
        {
            try
            {
                using (SD.Image OriginalImage = SD.Image.FromFile(Img))
                {
                    using (SD.Bitmap bmp = new SD.Bitmap(X2, Y2))
                    {
                        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, X2, Y2), X1, Y1, X2, Y2, SD.GraphicsUnit.Pixel);
                            MemoryStream ms = new MemoryStream();
                            bmp.Save(ms, OriginalImage.RawFormat);
                            return ms.GetBuffer();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                throw (Ex);
            }
        }
        }
    }
Posted

1 solution

To start with, the code
C#
try {
   SomeCodeHere();
} catch (System.Exception e) {
   throw e;
}

is strictly functionally equivalent to
C#
SomeCodeHere();

The only difference is some waste of CPU resources. In other words, catching this exception is this way is totally pointless. In your other exception catching case, you block further propagation of exception. This should be done rarely, in very few strategically chosen points of your application, which I call "competence points", where you know how to finally resolve the exception.

—SA
 
Share this answer
 
Comments
Member 10170389 7-Oct-13 2:49am    
Thanks for your answering and Iam new to Asp.net So please help me sir,Again Iam getteing the error i.e:
Line 94: using (SD.Image OriginalImage = SD.Image.FromFile(img))
Line 95: {
Line 96: using (SD.Bitmap bmp = new SD.Bitmap(X2, Y2))
Line 97: {
Line 98: bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
Sergey Alexandrovich Kryukov 7-Oct-13 2:54am    
Why would you change resolution at all? It is majorly irrelevant. In what line do you have an exception?
And use ms.ToArray instead...
—SA
Member 10170389 7-Oct-13 3:03am    
Sir ,Again Iam getting the Error i.e..Parameter is not valid at line 94 ... 96
Sergey Alexandrovich Kryukov 7-Oct-13 4:16am    
img should be a string with valid file name, a file should of be valid image type; X2, Y2 should be integers representing correct size, width and height. Compile a code fragment separately, to see what's wrong, use the debugger to see what's wrong during runtime.
—SA

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

  Print Answers RSS


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