locked
capture and crop image asp.net RRS feed

  • Question

  • User1408670804 posted

    i want to capture image with web cam and later crop the image

    Thursday, August 23, 2018 2:20 PM

All replies

  • User753101303 posted

    Hi,

    See https://www.html5rocks.com/en/tutorials/getusermedia/intro/

    For cropping do you mean something based on actual face detection such as https://cloudinary.com/blog/face_detection_based_cropping ?

    Thursday, August 23, 2018 5:55 PM
  • User-893317190 posted

    Hi lfeliz03 ,

    If you want to capture image with web cam you could use jQuery webcam plugin , please refer to https://www.xarg.org/project/jquery-webcam-plugin/.

    As to crop image , you could use gdi+.

    Below is my code.

    <form id="form1" runat="server">
            <asp:FileUpload ID="FileUpload1" runat="server"  />
            <asp:Image ID="Image2" runat="server"  ImageUrl="100.jpg"/>
            <asp:Image ID="Image1" runat="server" />
            <br />
           
            <br />
            please enter the x coordinate of the start point &nbsp;<asp:TextBox ID="TextBox1" runat="server" TextMode="Number"></asp:TextBox><br />
                please enter the y coordinate of the start point &nbsp;<asp:TextBox ID="TextBox2" runat="server" TextMode="Number"></asp:TextBox><br />
    
             please enter the width of the picture you want  <asp:TextBox ID="TextBox3" runat="server" TextMode="Number"></asp:TextBox>  <br />
            please enter the height of the picture you want  <asp:TextBox ID="TextBox4" runat="server" TextMode="Number"></asp:TextBox><br />
             <asp:Button ID="Button1" runat="server" Text="upload" />
    
        </form>
     

    Code behind.

     if (IsPostBack)
                {
    
                 //save the image
                    string file = Guid.NewGuid().ToString() + ".jpg";
                    string path = Request.MapPath("~/FileDemo/") + file;
                    FileUpload1.SaveAs(path);
                    int x = int.Parse(TextBox1.Text);
                        int y = int.Parse(TextBox2.Text);
                        int w = int.Parse(TextBox3.Text);
                        int h = int.Parse(TextBox4.Text);
    
                    
                        using (Bitmap map = new Bitmap(w, h))
                        {
                            using (Graphics g = Graphics.FromImage(map))
                            {
                                using (System.Drawing.Image img = System.Drawing.Image.FromFile(path))
                                {
                                                         
                                    g.DrawImage(img, new Rectangle(0, 0, w, h),  //specify the position and size of the new image
                                        new Rectangle(x, y, w, h),  //specify where to cut and how much to cut the original image
                                        GraphicsUnit.Pixel);
    
                                //save the image which has been cut
                                    string fileName = Guid.NewGuid().ToString() + ".jpg";
                                    map.Save(Request.MapPath("~/FileDemo/") + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                                //show the image which has been cut
                                    Image1.ImageUrl = "~/FileDemo/" + fileName;
    
                                }
    
                            }
    
    
                        }
                    
                  }
            }

    The result.

    Best regards,

    Ackerly Xu

    Friday, August 24, 2018 9:32 AM