how can i open an image and crop?

nadavrock

New member
Joined
Jul 20, 2019
Messages
2
Programming Experience
Beginner
how can i open an image and crop using c#? using visual studio windows forms
 
While I don't want to discourage people from using this forum when it's appropriate, forums should be your last resort, when you have looked for the information you need already out there and been unable to find it or unable to understand what you find. In this case, you appear not to have made the first effort to find existing information, because I just searched for "windows forms c# crop image" and the first match answers your question.


As I said, don't hesitate to post a question when you're stuck but do do what you can for yourself first and wait until you are actually stuck to post. It's in your own best interest to do so because, the more you look for information for yourself, the better you get at finding it, which means that you're not waiting for others to get around to answering you. You will also often find other useful information that you weren't even looking for. I speak from experience on both counts.
 
I will give you some pointers on the correct events to get you started making your cropping tool. Since I am rather pushed for time lately, I can't go into a lot of detail, but I am confident the folks here will help answer any questions you have If I'm not around this week. You can search the MSDN docs for references for the code below.

Alternatively you can also pick at some of the code form this project : Image Processing using C# - Further down this article, you will see some code for cropping images.

Below is how I'd approach making a cropping tool, and its really not that hard if you look at the Bitmap clone method.

Button1:
        private void Button1_Click(object sender, EventArgs e)
        {
            openFDiag.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*)|*.BMP;*.JPG;*.GIF;*.PNG;";
            if (openFDiag.ShowDialog() == DialogResult.OK)
            {
                eFile = openFDiag.FileName;
                pictureBox1.BackgroundImage = Image.FromFile(eFile);
                pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            }
        }
Declarations:
private int mouseMoveX, mouseMoveY; public static OpenFileDialog openFDiag = new OpenFileDialog(); public static string eFile;

Get Mouse Position From Mouse Events:
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
              //Check e. properties
        }
C#:
       private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            //Same as MouseDown, check e.Properties for mouse coordinates
        }
When Mouse Moves, Send Coordinates To New Thread And Update UI Though Delegating:
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            mouseMoveX = e.X; mouseMoveY = e.Y;
            ThreadStart updateThread = delegate
            {
                UpdateLabel(mouseMoveX, mouseMoveY);
            };
            new Thread(updateThread).Start();
        }
//The following will update the text on label1 which outputs the coordinates of your mouse pointer
private void UpdateLblText(string text)
        {
            label1.Text = text;
        }
//This delegate will be invoked to update the UI from the later invoking method which sits on the new thread
public delegate void lblCallback(string text);
        private void UpdateLabel(int mX, int mY)
        {
            Invoke(new lblCallback(UpdateLblText), new object[] { string.Concat(string.Concat(string.Concat("MOUSE COORDINATES :: MouseX.", mouseMoveX, " / MouseY.", mouseMoveY)))});
        }
Because the mouse move event fires every time the mouse moves, the coordinates will update the label depending on the position of your mouse. And so, I've run this event in a separate thread because I like to keep anything that involves interacting with or updating the UI on a separate or background thread. And this is common practice as well as my preference to update the UI from another thread, a preference you should adapt when working with anything that involves updating your UI, no matter how trivial or simple that change may be.

Lastly, with bitmap clone, you now have the documentation and source to dig in and try to get this working for yourself. The last piece of the pie is to implement the Bitmap Clone Method into what I've given you already. Documentation for doing so is found here : Bitmap.Clone Method (System.Drawing)

Hope you found this useful.
 
Back
Top Bottom