【发布时间】:2012-07-20 03:27:34
【问题描述】:
谁能告诉我如何以像素以外的任何单位从 GetBounds 取回一个矩形?以下代码 - 直接从 MSDN 文档中提取此函数 - 返回一个非常明显以像素而不是点(1/72 英寸)为单位的矩形。 (除非图标的大小为 32/72"x32/72" 而不是我认为的 32x32 像素)。我最感兴趣的是使用以英寸为单位的矩形,但我会满足于简单地看到 GetBounds pageUnit 参数导致返回的矩形发生变化。
Bitmap bitmap1 = Bitmap.FromHicon(SystemIcons.Hand.Handle);
Graphics formGraphics = this.CreateGraphics();
GraphicsUnit units = GraphicsUnit.Point;
RectangleF bmpRectangleF = bitmap1.GetBounds(ref units);
Rectangle bmpRectangle = Rectangle.Round(bmpRectangleF);
formGraphics.DrawRectangle(Pens.Blue, bmpRectangle);
formGraphics.Dispose();
【问题讨论】:
-
这是声明中的一个错误,参数是out,而不是ref。最好的办法就是使用宽度/高度。
-
否,尝试使用 'out' 关键字会产生错误。我转换单位没有什么困难,但如果它有效,这会更容易。
标签: c# image bitmap system.drawing system.drawing.imaging