I am using a zoomed picturebox. I want to retrieve the image Top-Left and Bottom-Right coordinates. But it is different from that of picturebox when the aspect ratio of the image doesn't match the picturebox. I wonder how I can get the image coordinates on the form.
Minus the Image size divided by 2 from the PictureBoxsize plus the Image size again.
This uses the Size.Subtract Method (Size, Size). MSDN
Size sizestep1 = Size.Subtract(new Size(PictureBox1.Image.Size.Width / 2, PictureBox1.Image.Size.Height / 2), PictureBox1.Size);
Size finalsize = Size.Add(sizestep1, PictureBox1.Image.Size);
// Convert to point.
Point BottomRightCoords = new Point(finalsize.Width, finalsize.Height);
And if you want to get the BottomRightCoords on the form, you have to add the PictureBox Location to it.
A little bit of mathematics suggested above + the code in the following link did the trick:
How to retrieve zoom factor of a WinForms PictureBox?
Related
I can create an image of an arbitrary size using this code:
using var image = original.Clone(context =>
context.Pad(width, height, Color.White)
);
However, this places the original image in the center of the new image. Is there a way to specify the location (e.g., using AnchorPositionMode)? I'd like the original image in the bottom right corner of the new image.
Pad as described in the docs is designed for evenly padding an image to fit the new dimensions.
You can combine Resize with ResizOptions to position padding but if you require fine grained control use DrawImage.
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)
I have a page in my MVC4 project where user can add its company logo using the file upload control. These images/logos are then shown on map in mobile application. We need to crop these images so that they can look like a Flag.
We need to take only the part of image inside the flag frame and leave the rest.
Can it be done using code in C#?
If yes, how it can be done. Please help me with some code samples and links.
I need to show a flag frame over the uploaded image, so that user can adjust its image in that frame, what it wants to be in the frame.
Please suggest me with some APIs and code samples.
Thanks.
Update: In some sites, when we upload profile image, it gives us a frame on top, and the image we have selected can be moved, so that the desired part comes into that frame. Now when we upload our profile image, it gets resized into that size. Can I do something similar here? in the frame above, I can give a flag shape, user can move the uploaded image, to get desired part of image in that frame.
Is it right approach?
How can we do this? I have looked into some jquery code samples, but no help.
You can use SetClip function with the Region as parameter:
https://msdn.microsoft.com/en-us/library/x1zb278e(v=vs.110).aspx
So you need to create Graphics object from Bitmap, set clip with the shape of your flag and then draw image on that Graphics object. That's all.
// Following code derives a cutout bitmap using a
// scizzor path as a clipping region (like Paint would do)
// Result bitmap has a minimal suitable size, pixels outside
// the clipping path will be white.
public static Bitmap ApplyScizzors(Bitmap bmpSource, List<PointF> pScizzor)
{
GraphicsPath graphicsPath = new GraphicsPath(); // specified Graphicspath
graphicsPath.AddPolygon(pScizzor.ToArray()); // add the Polygon
var rectCutout = graphicsPath.GetBounds(); // find rectangular range
Matrix m = new Matrix();
m.Translate(-rectCutout.Left, -rectCutout.Top); // translate clip to (0,0)
graphicsPath.Transform(m);
Bitmap bmpCutout = new Bitmap((int)(rectCutout.Width), (int)(rectCutout.Height)); // target
Graphics graphicsCutout = Graphics.FromImage(bmpCutout);
graphicsCutout.Clip = new Region(graphicsPath);
graphicsCutout.DrawImage(bmpSource, (int)(-rectCutout.Left), (int)(-rectCutout.Top)); // draw
graphicsPath.Dispose();
graphicsCutout.Dispose();
return bmpCutout;
}
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.
I need to be able to compare two different Images in the following way:
The images are displayed on top of each other so first there is only one image visible. When hovering with the mouse coursor over the image this defines the X-position of a vertical line which is splitting the image, displaying part of the first image left to the line and the rest of the second image right to the line.
This basically should be used as a quality comparison for two images with identical contents.
Here is a picture that hopefully makes my intentions clear:
you can use splitter control. splitter one side you put one picture control another side put other picture control
I was able to resolve this by using a SplitContainer and custom drawing. As described in the comment of Vasanthakumar's answer purely using a picturebox is not enough as you will not be able to align the right image at the same starting point than the left image (top left of the form).
What I did was the following:
pictureBox 1 displays its image normally
the Image in pictureBox 2 is drawn on every move of the splitter (this.splitContainer1.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.splitContainer1_SplitterMoved); with a custom subset of the Image to be displayed.
This effectively allows to generate the overlay I need.
Part of my implementation showing the drawing logic:
Bitmap bmp = new Bitmap(gImg2.Width, gImg2.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(gImg2, 0, 0, new Rectangle(e.SplitX, 0, gImg2.Width - e.SplitX, gImg2.Height), GraphicsUnit.Pixel);
}
pictureBox2.Image = bmp;