Background
I want to be able to get the drawn dimensions of a zoomed image inside the picturebox (I'll explain below).
The PictureBox.ImageRectangle property seems to be exactly what I'm looking for because it shows the height and width of the resized image, as well as it's relative top, left position inside the control.
The trouble is PictureBox.ImageRectangle is private and so I can't read the values without using reflection (which is obviously not ideal).
Actual Question
My question is, is there another way that I can easily get to these values without writing a method to calculate what the values "ought" to be? I can do that easily enough, but I feel I'd be reinventing the wheel.
Context:
I'm writing a simple image processing app in C# and one of the things it has to do is allow the user to draw a selection around a portion of the image (a lot like the Marquee tool in Photoshop).
I need to know the dimensions of the rendered image so I know where to set the bounds of my marquee tool and also to translate the values of the drawn rectangle to points on the scaled bitmap inside the control.
My answer look simple so maybe I'm missing something, but I think Control.DisplayRectangle suits your need.
EDIT
OK, missed the point; however see How to get the value of non- public members of picturebox?
if you want to access dimension of the image in picture box you can use
GraphicsUnit units = GraphicsUnit.Point;
RectangleF imgRectangleF = pictureBox1.Image.GetBounds(ref units);
Rectangle imgRectangle = Rectangle.Round(imgRectangleF);
Related
I'm able to fill a rectangle with an image and i apply a mask on top of the image using this code
args.DrawingSession.FillRectangle(rect, imgRnd, mask);
i need to apply some transform to this image, i'am able to do that with no issue, but i have encounter a strange issue, the last pixel is repeated.
i have used
imgRnd.ExtendX = CanvasEdgeBehavior.Wrap;
imgRnd.ExtendY = CanvasEdgeBehavior.Wrap;
and the image is repeated continuously.
My question is : there is a way to draw one time the image disabling and ExtendX and ExtendY?
FillRectangle will always fill all the pixels within the specified rectangle. The edge behavior enum controls what value they are filled with if the image is positioned such that it does not completely cover the rectangle being drawn.
How exactly are you transforming the image? Can you change that to also transform the rectangle itself, so you won't be trying to fill pixels that aren't covered by the image?
Another option is to use image effects (Microsoft.Graphics.Canvas.Effects namespace) which give much more detailed control than FillRectangle over how multiple images are transformed, combined, etc.
I am just started working in WPF application. Here, I need to get the signature from user. I am using Inkcanvas control to implement this feature.
I am curious on knowing is there a way in Inkcanvas control to crop the stroked region alone with some outer margin. I don't want the empty space of the Inkcanvas container in the output bitmap image. if the signature in too small, I want to crop the region and stretch that into particular size(300x200).
OK, first you need to determine the rectangle to crop (the "stroked region"), then save it to an image.
InkCanvas has a Strokes property that is the collection of ink strokes, you can get the bounds of each Stroke by calling Stroke.GetBound method. Then you can get the Left property of the "stroked region", which is the Left property of the leftmost bounds. And you also get the Right, Top and Bottom in the same way. I hope you can understand my explanation.
Resizing the cropped image to 300x200 should be quite easy, you can find plenty of answers on Stackoverflow.
I think this changed for Windows 10 Universal. In some ways, while different, it may be a bit easier.
With your InkCanvas, you have InkPresenter.StrokeContainer and from there you can get the BoundingRect property, which will give you a ton of details about the bounding rectangle of your strokes (ie x, y, width, height, left, right, etc...)
So, here is what I did:
var bounds = myCanvas.InkPresenter.StrokeContainer.BoundingRect;
var left = bounds.Left;
var right = bounds.Right;
//and so on...
Once you have the bounding rectangle data, it is very easy to crop.
Hope this helps!
I've got a large image in memory which I convert to an System.Windows.Media.ImageBrush and use it as the Fill for a System.Windows.Shapes.Rectangle. You can move this rectangle around with your cursor.
Basically I want to use the rectangle as a "viewport". Thus I need to change which parts of the image get displayed within the rectangle, i.e., define a rectangular subsection of the image.
How can I do that?
I see ImageBrush.Viewport but that doesn't seem to mean the same thing.
I'm open to alternative solutions that don't involve a rectangle, such as drawing directly on a canvas or something, but AFAIK WPF doesn't let you access pixel data directly (at least not easily).
To achieve this your going to have to create your own rectangle user control to allow the user to create/resize a rectangle. Then I would create a CroppedBitmap of the image in the rectangle portion Cropped Bitmap MSDN Stackoverflow example
Edit
No, no, no #Mark, You dont turn the CroppedBitmap into a UserControl. You create a USerControl that exposed the CroppedBitmap. Basically, you create a UserControl with the following DependencyProperties
The Image
The Width of he cropped portion
The Height of the cropped portion
The Left of the cropped portion
Top of the cropped portion
Then as soon as any of these properties your DP callback will do a RenderTargetBitmap Crop of the new region.
I am trying to write a simple program that lets me overlay a dot on top of an image when the image is clicked. I can save the X and Y data back to my database but then I will want to be able to call that information back at a later date and overlay the dots again via code unlike the first time when the user had to click the image.
I got as far as capturing the X and Y of the click no problem but I am having trouble finding examples specifically for what I am trying to do. All of the examples online seem to be for saving the image with the added graphic but I do not need to do that as it will be the same image every time.
Once I can do this, I also need to work out a way that I can detect what area of the image has been clicked. The areas I need to mark out vary in shape and size so I need to try and work out a way to 'map' these areas and then cross reference with the co-ordinates of the users click (I assume that I may need to do some clever geometry stuff for that?)
If anyone has any suggestions of what subjects/classes/methods etc. to research for either of my queries, I would very grateful.
Thanks in advance
You can use the System.Drawing namespace to achieve this.
Create a control and override OnPaint and OnPaintBackground. Store your clicks in a List
In OnPaintBackground, draw the image using DrawImageUnscaled using the graphics object which is passed to you as a parameter.
In OnPaint, loop through your points array and call graphics.FillElipse or similar to draw a little dot.
Because this isnt a retained mode graphics system, you need to keep drawing these items so this may not suit a large number of dots. In that case, you can create an in memory bitmap and get a graphics drawing object using graphics.FromImage.
I'm building a c# app to take photos of visitors for ID badges. I have found a c# wrapper for web cams so can take a snap shot easily, however I need to set the images to a certain number of pixels ideally with the persons head filling most of the area.
I imagine the best way to do this is to allow the end user to drag out a rectangle over the initial image with fixed x and y ratio covering the persons face. This would crop the image and do any resolution adjustment necessary.
Does anyone know of an example similar to this? I think I know how to do the rectangle and get my co-ordinates for the image cropping apart from fixing an x and y ratio. Obviously an example would be quicker than trying to write my own.
As of .Net 3.0, System.Windows.Media.Imaging provides a CroppedBitmap class that inherits from BitmapSource. Its constructor takes a BitmapSource and an Int32Rect. If you pass it your original image and your rect, it will return a cropped image as a BitmapSource.