Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Crop image #38

Open
lordofscripts opened this issue Sep 5, 2020 · 1 comment
Open

Unable to Crop image #38

lordofscripts opened this issue Sep 5, 2020 · 1 comment

Comments

@lordofscripts
Copy link

This is an excellent and versatile control, I am using the ImageBoxEx code (v1.1) extending the ImageBox v1.4 found in the latest NuGet package. I am using ImageBoxEx because I want to add some image editing capabilities to transform my image.

So far I get my image displayed, the selection regions are displayed and can be resized and moved. At some point though, I want to CROP the image using the SelectionRegion rectangle. I have the code to do the cropping which I have tested in a regular (stock) PictureBox control; However, this does not seem to be working with the ImageBox/Ex controls.

Basically I do this:

using (ImageProcessor imgProc = new ImageProcessor(imageBox1.Image))
{
  Rectangle rect = Rectangle.Round(imageBox1.SelectionRegion);
  imageBox1.SelectNone();
  imgProcessor.Crop(rect);
  imageBox1.Image = imgProcessor.CurrentImage;
}

where the Crop method looks like this:

public void Crop(Rectangle r)
{
   Bitmap temp = (Bitmap)_currentBitmap;   // this is where the bitmap was placed by the constructor
   Bitmap bmap = (Bitmap)temp.Clone();
   if (r.X + r.Width > _currentBitmap.Width)
       r.Width = _currentBitmap.Width - r.X;
   if (r.Y + r.Height > _currentBitmap.Height)
       r.Height = _currentBitmap.Height - r.Y;
   _currentBitmap = (Bitmap)bmap.Clone(r, bmap.PixelFormat);
}

But after I do that the imageBox image gets cleared from the ImageBoxEx control. If I eliminate the using() then the image remains the same as if I had not cropped it.

@lordofscripts
Copy link
Author

Looking on the web I ran across another article from you that happened to do just that with your ImageBox:
https://www.cyotek.com/blog/creating-an-image-viewer-in-csharp-part-5-selecting-part-of-an-image

I tried it that way and it worked. I don't know why the original approach works on the PictureBox but not on ImageBox, maybe because yours is more complex but MUCH more versatile! Excellent work by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant