有点晚了回答这个问题,但我觉得要回答这个问题:“我有多少的mm可以适合我的图片框的时候我会做,因为我发现它在谷歌? “,这将为我节省大量时间,而不必制定如何去做!的getBounds是无用的(如果你想它以像素为单位...),但它是可以找到使用Graphics.TransformPoints方法绘图单位和显示像素之间的关系:
private void Form1_Load(object sender, EventArgs e)
{
Bitmap b;
Graphics g;
Size s = pictureBox1.Size;
b = new Bitmap(s.Width, s.Height);
g = Graphics.FromImage(b);
PointF[] points = new PointF[2];
g.PageUnit = GraphicsUnit.Millimeter;
g.PageScale = 1.0f;
g.ScaleTransform(1.0f, 1.0f);
points[0] = new PointF(0, 0);
points[1] = new PointF(1, 1);
g.TransformPoints(CoordinateSpace.Device, CoordinateSpace.Page, points);
MessageBox.Show(String.Format("1 page unit in {0} is {1} pixels",g.PageUnit.ToString(),points[1].X));
points[0] = new PointF(0, 0);
points[1] = new PointF(1, 1);
g.TransformPoints(CoordinateSpace.Page, CoordinateSpace.World, points);
MessageBox.Show(String.Format("1 page unit in {0} is {1} pixels",g.PageUnit.ToString(),points[1].X));
g.ResetTransform();
pictureBox1.Image = b;
SolidBrush brush = new SolidBrush(Color.FromArgb(120, Color.Azure));
Rectangle rectangle = new Rectangle(10, 10, 50, 50);
// Fill in the rectangle with a semi-transparent color.
g.FillRectangle(brush, rectangle);
pictureBox1.Invalidate();
}
这将显示基本毫米显示像素(在我的情况下是3.779527) - 世界坐标是每像素1毫米,如果你应用graphics.ScaleTransform,这将会改变。
编辑:当然,如果将位图分配给pictureBox图像属性(并使Graphics对象允许根据需要进行更改),它会有所帮助。
这是声明中的一个错误,参数是* out *,not * ref *。最好的做法是使用宽度/高度。 – 2012-07-21 00:12:21
不,尝试使用'out'关键字会产生错误。我很难转换单位,但如果它有效,这会更容易。 – 2012-08-08 15:46:25