Results 1 to 8 of 8

Thread: I have a question about how crop Image

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    5

    I have a question about how crop Image

    I found many explanations in crop Image
    but they all rotate to crop one image at a time

    If I have a large number of pictures to crop by the same measurement
    What advice do you have to edit on this code so that it will be executed once or automatically

    Code:
    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 System.Drawing;
    
    public partial class CropAndSaveImageCSharp : System.Web.UI.Page
    {
        protected void btnUpload_Click(object sender, EventArgs e)
        {
    
        }
        
            
            
            string fileName = string.Empty;
            string filePath = string.Empty;
            string extension = string.Empty;
            try
            {
                //Check if Fileupload control has file in it
                if (FileUpload1.HasFile)
                {
                    // Get selected image extension
                    extension = Path.GetExtension(FileUpload1.FileName).ToLower();
                    //Check image is of valid type or not
                    if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
                    {
                        //Cretae unique name for the file
                        fileName = Guid.NewGuid().ToString() + extension;
                        //Create path for the image to store
                        filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
                        //Save image in folder
                        FileUpload1.SaveAs(filePath);
                        //Show the panel and load the uploaded image in image control.
                        pnlCrop.Visible = true;
                        imgToCrop.ImageUrl = "~/Images/" + fileName;
                    }
                    else
                    {
                        lblMsg.Text = "Please select jpg, jpeg, png, gif or bmp file only";
                    }
                }
                else
                {
                    lblMsg.Text = "Please select file to upload";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "Oops!! error occured : " + ex.Message.ToString();
            }
            finally
            {
                extension = string.Empty;
                fileName = string.Empty;
                filePath = string.Empty;
            }
        }
    
        protected void btnCrop_Click(object sender, EventArgs e)
        {
            string croppedFileName = string.Empty;
            string croppedFilePath = string.Empty;
            //get uploaded image name
            string fileName = Path.GetFileName(imgToCrop.ImageUrl);
            //get uploaded image path
            string filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
    
            //Check if file exists on the path i.e. in the UploadedImages folder.
            if (File.Exists(filePath))
            {
                //Get the image from UploadedImages folder.
                System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath);
                //Get user selected cropped area
                //Convert.ToInt32(String.Format("{0:0.##}", YCoordinate.Value)),
    
                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);
                    //Create graphics object for alteration
                    using (Graphics g = Graphics.FromImage(bitMap))
                    {
                        //Draw image to screen
                        g.DrawImage(orgImg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), areaToCrop, GraphicsUnit.Pixel);
                    }
    
                    //name the cropped image
                    croppedFileName = "crop_" + fileName;
                    //Create path to store the cropped image
                    croppedFilePath = Path.Combine(Server.MapPath("~/Images"), croppedFileName);
                    //save cropped image in folder
                    bitMap.Save(croppedFilePath);
                    orgImg.Dispose();
                    bitMap = null;
                    //Now you can delete the original uploaded image from folder                
                    File.Delete(filePath);
                    //Hide the panel
                    pnlCrop.Visible = false;
                    //Show success message in label
                    lblMsg.ForeColor = Color.Green;
                    lblMsg.Text = "Image cropped and saved successfully";
    
                    //Show cropped image
                    imgCropped.ImageUrl = "~/Images/" + croppedFileName;
    
                    //Show Reset button
                    btnReset.Visible = true;
                }
                catch (Exception ex)
                {
                    lblMsg.Text = "Oops!! error occured : " + ex.Message.ToString();
                }
                finally
                {
                    fileName = string.Empty;
                    filePath = string.Empty;
                    croppedFileName = string.Empty;
                    croppedFilePath = string.Empty;
                }
            }
    
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            imgCropped.ImageUrl = "";
            lblMsg.Text = string.Empty;
            btnReset.Visible = false;
        }
    }

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    5

    Re: I have a question about how crop Image

    This is the project
    Please help modify it

    If I have a large collection of Image and I want to make crop and save
    Is there a way to do that?
    So that they are cut once with the same measurements

    Name:  1-2.jpg
Views: 236
Size:  23.7 KB
    Last edited by kha.kha; Jul 20th, 2018 at 11:55 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    5

    Re: I have a question about how crop Image

    Hello owners
    Now 3 days ..... I am waiting for any comment
    Is my question unclear or what?

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: I have a question about how crop Image


  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: I have a question about how crop Image

    Quote Originally Posted by kha.kha View Post
    If I have a large number of pictures to crop by the same measurement
    What advice do you have to edit on this code so that it will be executed once or automatically
    Put all your images into a list and then call Parallel.ForEach to process multiple items in parallel. As a simple example, this:
    csharp Code:
    1. var imageFilePaths = new List<string>();
    2.  
    3. // ...
    4.  
    5. foreach (var imageFilePath in imageFilePaths)
    6. {
    7.     ProcessImageFile(imageFilePath);
    8. }
    becomes this:
    csharp Code:
    1. var imageFilePaths = new List<string>();
    2.  
    3. // ...
    4.  
    5. Parallel.ForEach(imageFilePaths, ProcessImageFile);

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: I have a question about how crop Image

    I think I may have misunderstood the question but it's still a good idea to use parallel processing to speed things up. The thing is, I'm not sure why the question is being asked in the first place. If you have a method that crops an image in a specific way then you simply call that method for multiple images, whether in serial or parallel, and they will all be cropped in that specific way. I'm not seeing what the actual problem is.

    Maybe you didn't actually think to move the cropping code to its own method. If not, that's your solution. You can do that in your existing single-image code first, to make sure that it works as expected. You can then simply call that same method multiple times for multiple images.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    5

    Re: I have a question about how crop Image

    In the beginning
    Thank you, Mr. #jdc2000
    But this is not the problem .... The problem is how how crop all Images (same size crop on all images)


    and Thank you, Mr. #jmcilhinney
    I will explain the idea again
    ?????????????????
    If I have a lot of bills..... like this
    Name:  1-1.jpg
Views: 276
Size:  44.5 KB

    and I want to crop some information out of it as Image
    example, shipping information

    Name:  1-2.jpg
Views: 293
Size:  23.7 KB



    How do I do it at one time for all images and same size crop



    @jdc2000 @jmcilhinney

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: I have a question about how crop Image

    It seems like you ignored what I did say. If you can already do this for one image then you basically have all you need. Just refactor your code so that the code that does the cropping is in its own method and you just pass in what it needs: either the Image itself or the path of an image file. Once that is working with your current single image, you can do as I showed and call that method in a loop to process multiple images. You can use a regular loop and process the images in serial or call Parallel.ForEach and process them in parallel. I suggest that you follow those instructions and post back if you encounter any specific issues along the way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width