Crop Image in ASP.net

Last post 07-09-2009 4:17 AM by sandy060583. 9 replies.

Sort Posts:

  • Crop Image in ASP.net

    03-11-2009, 7:35 AM
    • Member
      point Member
    • junlo
    • Member since 01-30-2008, 6:40 AM
    • Posts 21

     I would like to have a function to upload image and crop the image into thumbnail ( The size for thumbnail is fixed.)

    This is one of the example that i found:

    http://michael.sivers.co.uk/post/2007/08/20/Crop-and-resize-an-image-in-ASPNET.aspx

    But the problem is :

    1. How can i let the user to determine which the area to crop it? ( like the gmail, user can select the area of image and crop  the selected area only)

     Besides, anybody know how to use the jQuery, JCrop?

    Thanks.

     

     

     

  • Re: Crop Image in ASP.net

    03-11-2009, 8:04 AM
    • Star
      11,983 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,360

    Read here.

    Shahed Kazi [SCJP]

    Read My Blog
  • Re: Crop Image in ASP.net

    03-11-2009, 8:53 AM
    Answer
    • Participant
      1,832 point Participant
    • SurendraKishore
    • Member since 03-06-2009, 1:24 PM
    • Hyderabad
    • Posts 338

    Make use of this free control http://webcropimage.codeplex.com/

    this allows user to select the crop size and do it.

    else you can make use of this simple function:

    1. Private Function CropImage(ByVal OriginalImage As Bitmap, ByVal TopLeft As Point, ByVal BottomRight As Point) As Bitmap
    2.     Dim btmCropped As New Bitmap((BottomRight.Y - TopLeft.Y), (BottomRight.X - TopLeft.X))
    3.     Dim grpOriginal As Graphics = Graphics.FromImage(btmCropped)
    4.     grpOriginal.DrawImage(OriginalImage, New Rectangle(0, 0, btmCropped.Width, btmCropped.Height), _
    5.         TopLeft.X, TopLeft.Y, btmCropped.Width, btmCropped.Height, GraphicsUnit.Pixel)
    6.     grpOriginal.Dispose()
    7.     Return btmCropped
    8. End Function

    Usage:

    1. Dim bmpImage As New Bitmap("c:\point.jpg")
    2. bmpImage = CropImage(bmpImage, New Point(0, 0), New Point(100, 100))
    MARK AS ANSWER IF MY POST HELPED YOU!
    THIS REDUCES TIME FOR OTHERS..!
  • Re: Crop Image in ASP.net

    03-11-2009, 10:56 PM
    • Member
      point Member
    • junlo
    • Member since 01-30-2008, 6:40 AM
    • Posts 21

    i would like to have a preview function like below:

    http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail

    I want to set the thumbnail size to "62 x62".

    But, i having problem to crop the selected area when i change

    "onChange: storeCoords,"  to "onChange: showPreview," 

    How can i have a preview function and crop the preview area in "62x62"?

    Appreciate someone can provide the code example, thanks.

     

     

  • Re: Crop Image in ASP.net

    03-12-2009, 11:49 AM
    • Participant
      1,832 point Participant
    • SurendraKishore
    • Member since 03-06-2009, 1:24 PM
    • Hyderabad
    • Posts 338

    can you paste your code here, lemme have a look on it.

    MARK AS ANSWER IF MY POST HELPED YOU!
    THIS REDUCES TIME FOR OTHERS..!
  • Re: Crop Image in ASP.net

    03-12-2009, 6:29 PM
    • Member
      point Member
    • junlo
    • Member since 01-30-2008, 6:40 AM
    • Posts 21

    Here is my code ( without preview):

    <link href="includes/jquery.Jcrop.css" rel="stylesheet" type="text/css" />
       <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
        <script type="text/javascript" src="js/jquery.Jcrop.min.js"></script>
        <script type="text/javascript" src="js/jquery.pack.js"></script>
        <script src="js/jquery.Jcrop.js" type="text/javascript"></script>
        <script src="js/jquery.Jcrop.pack.js" type="text/javascript"></script>
    <script type="text/javascript">
    
        jQuery(document).ready(function() {
        jQuery('#imgCrop').Jcrop({
                onSelect: storeCoords,
                aspectRatio: 1
            });
        });
    
    
        function storeCoords(c) {
    
            jQuery('#X1').val(c.x);
            jQuery('#Y1').val(c.y);
            jQuery('#W1').val(c.w);
            jQuery('#H1').val(c.h);
    
        };
    
    
    
    </script>
     <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"/>
    
          <br />
    
          <asp:HiddenField ID="X1" runat="server" />
    
          <asp:HiddenField ID="Y1" runat="server" />
    
          <asp:HiddenField ID="W1" runat="server" />
    
          <asp:HiddenField ID="H1" runat="server" />
    
          <asp:Button ID="btnCrop" runat="server" Text="Crop" OnClick="btnCrop_Click" />
    
        </asp:Panel>
    
        <asp:Panel ID="pnlCropped" runat="server" Visible="false">
    
          <asp:Image ID="imgCropped" runat="server" />
    
        </asp:Panel>
      
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.IO
    Imports SD = System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Drawing2D
    
    Partial Class photo_upload
        Inherits System.Web.UI.Page
    
        Dim path As String = HttpContext.Current.Request.PhysicalApplicationPath + "images\upload\"
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
        End Sub
    
    
        Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim FileOK As [Boolean] = False
    
            Dim FileSaved As [Boolean] = False
    
    
    
            If Upload.HasFile Then
    
                Session("WorkingImage") = Upload.FileName
    
                Dim FileExtension As String = System.IO.Path.GetExtension(Session("WorkingImage").ToString()).ToLower()
    
                Dim allowedExtensions As [String]() = {".png", ".jpeg", ".jpg", ".gif"}
    
                Dim i As Integer = 0
                While i < allowedExtensions.Length
                    If FileExtension = allowedExtensions(i) Then
                        FileOK = True
                    End If
                    System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
    
                End While
            End If
    
    
    
            If FileOK Then
    
                Try
    
                    Upload.PostedFile.SaveAs(path + Session("WorkingImage"))
    
    
                    FileSaved = True
                Catch ex As Exception
    
    
                    lblError.Text = "File could not be uploaded." + ex.Message.ToString()
    
                    lblError.Visible = True
    
    
                    FileSaved = False
    
                End Try
            Else
    
    
                lblError.Text = "Cannot accept files of this type."
    
    
                lblError.Visible = True
            End If
    
    
    
            If FileSaved Then
    
                pnlUpload.Visible = False
                pnlCrop.Visible = True
                imgCrop.ImageUrl = "images/upload/" + Session("WorkingImage").ToString()
            End If
    
    
    
    
    
        End Sub
    
        Protected Sub btnCrop_Click(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim ImageName As String = Session("WorkingImage").ToString()
    
            Dim w As Integer = Convert.ToInt32(W1.Value)
    
            Dim h As Integer = Convert.ToInt32(H1.Value)
    
            Dim x As Integer = Convert.ToInt32(X1.Value)
    
            Dim y As Integer = Convert.ToInt32(Y1.Value)
    
    
    
            Dim CropImage As Byte() = Crop(path + ImageName, w, h, x, y)
    
            Using ms As New MemoryStream(CropImage, 0, CropImage.Length)
    
                ms.Write(CropImage, 0, CropImage.Length)
    
                Using CroppedImage As SD.Image = SD.Image.FromStream(ms, True)
    
                    Dim SaveTo As String = path + "crop_" + ImageName
    
                    CroppedImage.Save(SaveTo, CroppedImage.RawFormat)
    
                    pnlCrop.Visible = False
    
                    pnlCropped.Visible = True
    
    
                    imgCropped.ImageUrl = "images/upload/crop_" + ImageName
    
                End Using
            End Using
    
        End Sub
    
        Shared Function Crop(ByVal Img As String, ByVal Width As Integer, ByVal Height As Integer, ByVal X As Integer, ByVal Y As Integer) As Byte()
    
            Try
    
                Using OriginalImage As SD.Image = SD.Image.FromFile(Img)
    
                    Using bmp As New SD.Bitmap(Width, Height, OriginalImage.PixelFormat)
    
                        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution)
    
                        Using Graphic As SD.Graphics = 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)
    
                            Dim ms As New MemoryStream()
    
                            bmp.Save(ms, OriginalImage.RawFormat)
    
    
                            Return ms.GetBuffer()
    
                        End Using
    
                    End Using
    
                End Using
            Catch Ex As Exception
    
    
    
                Throw (Ex)
            End Try
    
        End Function
    
       
    
    End Class
     
    Besides the preview problem, i also face another problem.
    When i upload a big photo, let said the size is (1500 X1200). The photo will display at 1500 X 1200 after uploaded, how can i make it display in small size with respect ratio ( width: 450)?
    Because when the photo size is too big (full on the screen), it is difficult to crop it and affected my design page. 
     

     

  • Re: Crop Image in ASP.net

    03-15-2009, 11:21 PM

    junlo:
    Besides the preview problem, i also face another problem.
    When i upload a big photo, let said the size is (1500 X1200). The photo will display at 1500 X 1200 after uploaded, how can i make it display in small size with respect ratio ( width: 450)?
    Because when the photo size is too big (full on the screen), it is difficult to crop it and affected my design page. 

    For this problem, I suggest you generate a thumbnail after uploading an image. If in this case, you can govern the size of the Image.

    Gary yang - MSFT
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Crop Image in ASP.net

    03-16-2009, 10:40 PM
    Answer
    • Contributor
      3,814 point Contributor
    • Tomorrow
    • Member since 12-10-2008, 6:39 AM
    • Posts 400

    junlo:
    Besides the preview problem, i also face another problem.
    When i upload a big photo, let said the size is (1500 X1200). The photo will display at 1500 X 1200 after uploaded, how can i make it display in small size with respect ratio ( width: 450)?
    Because when the photo size is too big (full on the screen), it is difficult to crop it and affected my design page. 

    Dear Junlo,

    I found a great demo for corping image in asp.net2.0(even if it is resolved in C#). I think it can help you to address the issue.

    http://webcropimage.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=20913

    as you metioned that you worry about the size of Upload image, I think you can refer to the gary's advice to generate a thumbnail proportional 。

  • Re: Crop Image in ASP.net

    05-19-2009, 12:00 PM

     http://nathanaeljones.com/products/asp-net-image-resizer/

    Once installed, you can size, crop, and change the format of images from anywhere... HTML, ASP.NET, ASP, PHP, Flex, or Flash ... Just add the desired behavior to the URL with "?width=100" or "?format=jpg". It supports scaling, cropping, rotating, flipping, stretching, padding, borders, transparency, jpeg, png, and gif formats. Aspect ratio is always maintained unless &stretch=fill is specified.

    Also read http://nathanaeljones.com/11191_20_Image_Resizing_Pitfalls

  • Re: Crop Image in ASP.net

    07-09-2009, 4:17 AM
    • Contributor
      2,147 point Contributor
    • sandy060583
    • Member since 05-23-2007, 12:49 AM
    • Ahmedabad
    • Posts 479

    hi visit the following link...

    & download the user control for croping image...

    http://webcropimage.codeplex.com/


    Please mark the answer if it helps you

    Ramani Sandeep
    DotNetting – Fast , Easy Way of Developing Applications


Page 1 of 1 (10 items)