Cyotek.Windows.Forms.ImageBox icon indicating copy to clipboard operation
Cyotek.Windows.Forms.ImageBox copied to clipboard

Unable to Crop image

Open lordofscripts opened this issue 5 years ago • 1 comments

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 avatar Sep 05 '20 21:09 lordofscripts

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.

lordofscripts avatar Sep 05 '20 23:09 lordofscripts