Get selected portion of image in picturebox in another picturebox - c#

I have a picturebox with image, in which I have drawn a rectangular selection. I want to get the selected portion of image in another picturebox. How can I get that? Please help.

Suppose the rect is the Rectangle which you passed in the Graphics.DrawRectangle is calculated in your the coordinates of your pictureBox. You can use RectangleToScreen and RectangleToClient to get that portion in another pictureBox like this:
Rectangle portion = pictureBox2.RectagleToClient(pictureBox1.RectangleToScreen(rect));
//portion is the Rectangle calculated in the coordinates of pictureBox2.

Use coordinates of the selection to create Rectangle rectangle then:
Bitmap sourceBitmap = new Bitmap(pictureBoxImage);
Bitmap croppedBitmap = sourceBitmap.Clone(rectangle, sourceBitmap.PixelFormat);
after that you may use croppedBitmap in another picturebox. Don't forget to dispose unused images. That's all.

Related

How can I make the picture smaller in the picture box without affecting the coordination?

I want to let the user define the area that they want to extract the text, but for the line item, the picture is too large and it make the rectangle cannot include the whole table.
I found a solution that able to smaller the picture but the coordinate also changed. For example, I draw the rectangle area at the "Malarvili" but the rectangle coordinate is not same as what I had drawn. Because of this, the extracted text is wrong.
So I want to know any solution to make the picture becomes smaller in the picturebox without affecting the original rectangle coordinate? Or using my original solution which is using the "autosize" without changing the picture size, but I have also facing a problem which is when i drawing the rectangle to the right, the scrollbar will not autoscroll and it makes me cannot draw the rectangle to the end of the right.
Any solution to solve these problems? Thanks a lot.
You can calculate the ratios (x and y axis) between the resize image (pictureBox with StretchImage mode) and the original image, and then calculate back the rectangle for original image from the rectangle you drawed in the pictureBox using these ratios.
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
ratio_X = (double) pictureBox.Width/original_image;
ratio_Y = (double) pictureBox.Height/original_image;
//suppose you have the rect drawed in the pictureBox: pictureBox_rect
//now make a rect for original_image
Rectangle original_rect = new Rect((int)pictureBox_rect.X * ratio_X,
(int)pictureBox_rect.Y * ratio_Y,
(int)pictureBox_rect.Width * ratio_X,
(int)pictureBox_rect.Height * ratio_Y)

Undraw rectangle on an image

I have a custom control that displays an image. I'm using a custom control because I've implemented zooming functionality and scrollbars.
I'm draw a rectangle on the image by doing:
if (drawRect)
{
var g = Graphics.FromImage(Image);
g.DrawRectangle(Pens.Red, rect);
}
The drawing of the rectangle is toggled by a button. However, when I toggle off, the rectangle it's still being drawn on the image.
I'm assuming that once something is drawn on the image's surface, it stays there.
Is there a way to "undraw" something or do I need to implement a second "transparent" image that will act as an overlay?

C# user control's background image

I have a custom control that I'm inheriting from Control. I need to display an icon so I'm using the available properties (BackgroundImage and BackgroundImageLayout).
I select an image and set the layout to None. However, I don#t want that the image is draw in the top-left corner of the control, but in another location that I specify.
Is this possible?
Try something like this inside your OnPaint();
Image myBackgroundImage = Image.FromFile("xyz.jpg");
e.Graphics.DrawImage(myBackgroundImage, myX, myY, new Rectangle(0, 0, myBackgroundImage.Width, myBackgroundImage.Height), GraphicsUnit.Pixel);
You're telling the Graphics object to draw your image at the specific location of myX and myY. The rectangle is the portion of the image to draw, in this case the whole image.

C# Remove drawing from PictureBox

I have a PictureBox pic , and an Image img, pic.Image = img
and I draw some rectangles on the Image using
Graphics g = Grpahics.FromImage(pic.Image);
g.DrawRectangle(...);
But at one point, I want to remove the rectangle from the Image,I tried
pic.Image = getOriginalImage();
pic.Refresh();
but the image remains the same(Rectangles are still on top of the Image)
I know there's a method graphics.Clear(Color),but it replace the entire Image with a solid color
How do I remove the drawing only from the PictureBox?
Thanks
Edit:
I already saved the original and when I erase I use the original image
You are overwriting the original image. Save the image locally and clone it into the PictureBox.Image property. Then clone again when you want to erase
Draw the background image again using Graphics.DrawImage() or assign the image again to the Image property. Using a Graphics object in this way is writing directly to the image displayed in the PictureBox and it does not keep separate copies of what is written using a Graphics object and the background Image.

How to draw a rectangle on picturebox and save selected part as .jpg

How to draw a rectangle on picturebox and save selected part as .jpg.
Well i know how to draw a rectange on picturebox, but i don't know how part of picture inside this rectangle can be saved as for example new .jpg..
If your image is always perfectly fited in picture box, you can create Graphics from Image class
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx
And after use Graphics http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimageunscaledandclipped.aspx DrawImageUnscaledAndCliped method to clip your image in memory. After this you can save that Image to file.
If image is not fitted so you use SizeMode picturebox property, you need to calculate a proportion of drawn rectangle in regard of picture box rectangle and after apply that proportion to image real rectangle.

Categories