I'm having some trouble displaying an image in a PictureBox in my Windows Forms application. I want to zoom and move the Bitmap inside the PictureBox. I have an object RectangleF
that hold the location and the size of the Bitmap inside the PictureBox.
I need to process the Bitmap before to assign to the PictureBox and for this reason I'm using a method called Apply to create a new Bitmap image from an existing one and then trying to display it in the PictureBox.
This is the code:
public Bitmap Apply(Bitmap image)
{
//_drawingRect contains the location of the Bitmap in the PictureBox and the size of the Bitmap
//The result image
Bitmap result = new Bitmap((int)_drawingRect.Width, (int)_drawingRect.Height);
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.SetClip(_clipRect);
graphics.DrawImage(image, _drawingRect, new RectangleF(0,0 , image.Width, image.Height), GraphicsUnit.Pixel);
return result;
}
}
I use the method in this way:
myPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
myPictureBox.Image = Apply(myBitmap);
myPictureBox.Refresh();
The image is being created and displayed successfully the first time but when I move the Image inside the PictureBox or when I zoom the Image, _drawingRect
will change correctly, but the Image is cropped.
I think the problem could be the setting of myPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
Thanks in advice