背景
我希望能够获取图片框中缩放图像的绘制尺寸(我将在下面解释)。
PictureBox.ImageRectangle
属性似乎正是我正在寻找的,因为它显示了调整大小的图像的高度和宽度,以及它在控件内的相对顶部、左侧位置。
问题是 PictureBox.ImageRectangle
是私有的,因此我无法在不使用反射的情况下读取这些值(这显然不理想)。
实际问题
我的问题是,是否有另一种方法可以轻松获取这些值,而无需编写方法来计算值“应该”是什么?我可以很容易地做到这一点,但我觉得我会重新发明轮子。
上下文:
我正在用 C# 编写一个简单的图像处理应用程序,它必须做的一件事是允许用户围绕图像的一部分绘制选区(很像选框工具)在 Photoshop 中)。
我需要知道渲染图像的尺寸,以便知道在哪里设置选取框工具的边界,并将绘制的矩形的值转换为控件内缩放位图上的点。
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.
发布评论
评论(2)
我的答案看起来很简单,所以也许我遗漏了一些东西,但我认为 Control.DisplayRectangle 适合您的需要。
编辑
好,错过了重点;但是请参阅如何获取非值picturebox 的公共成员?
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