开发者

How do I get the dimensions of the ImageRectangle in PictureBox?

开发者 https://www.devze.com 2023-04-08 18:36 出处:网络
Background I want to be able to get the drawn dimensions of a zoomed image inside the picturebox (I\'ll explain below).

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号