【问题标题】:System.Drawing.Bitmap GetBounds GraphicsUnit.InchSystem.Drawing.Bitmap GetBounds GraphicsUnit.Inch
【发布时间】: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


【解决方案1】:

这方面的信息有点稀疏,我能找到这个MSDN Forum posting,这表明由于位图已经创建,单位已经设置并且不可更改。由于GraphicsUnit 是通过引用传递的,因此您在调用后查看它会发现它从Inch 设置回Pixel。如果您确实想更改绘制矩形的大小,请将formGraphics 上的Graphics.PageUnit Property 设置为您想要绘制矩形的GraphicsUnit

来自以上链接:

在这个示例中,Image.GetBounds 方法的参数不会改变结果,因为 Bitmap 的边界已经确定。参数只确定单位长度处理的范围,逐英寸或逐点。但参数不会影响结果。

强调我的

【讨论】:

  • 当 MSDN 文档示例对示例中使用的图像进行硬编码并且他们知道该图像的单位时,将显式初始化单元以“指向”是没有任何意义的。此外,GetBounds 方法的文档说:以指定单位获取图像的边界。这几乎可以肯定是一个(另一个)GDI+ 错误。知道分辨率的单位转换没问题,但我发现大图像的错误,放弃了,开始改用 WPF。
【解决方案2】:

回答这个有点晚了,但我想我会这样做,因为我在尝试回答“我的图片框可以容纳多少毫米?”的问题时在 Google 中找到了它,它会为我节省很多时间不必弄清楚如何去做! 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();

    }

这将显示基本 mm 以显示像素(在我的情况下为 3.779527) - 世界坐标为每像素 1 mm,如果您应用 graphics.ScaleTransform,这将改变。

编辑:当然,如果您将位图分配给图片框图像属性(并保留 Graphics 对象以允许根据需要进行更改)会有所帮助。

【讨论】:

    【解决方案3】:

    添加标签 在 Form1 类中添加字段 PointF[] 坐标; Form1.cs [设计] 在属性中查找照明螺栓双击绘制创建处理程序 Form1_Paint(对象发送者,PaintEventArgs) { e.Graphics.PageUnit = GraphicsUnit.Inch; 如果(坐标!= null) e.Graphics.TransformPoints(CoorinateSpace.World, CoorinateSpace.Device,坐标); } 再次为 Form1.MouseMove 创建处理程序 Form1_MouseMove(对象发送者,MouseEventArgs e { 坐标[0].X = e.Location.X; 坐标[0].Y = e.Location.Y; this.Refresh(); label1.Text = $"X = {cooridates[0].X} Y = { { 坐标[0].Y } "; }

                                                                                Form1_Load(object sender,MouseEventArgs)
                                                                                    {
                                                                                        cooridates = new PointF[1] { new PointF(0f,0f) };
                                                                                             }
    
                                                                                                 Move mouse to get cooridates in Inches
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2023-03-24
      相关资源
      最近更新 更多