System.Drawing.Bitmap GetBounds GraphicsUnit.Inch

Can someone tell me how to get a rectangle from GetBounds in any units except pixels? The following code, taken directly from the MSDN documentation for this function, returns a rectangle that is obviously in pixels, not dots (1/72 of an inch). (If the icons do not have a size of 32/72 "x32 / 72" and not 32x32 pixels, as I think). I'm most interested in working with a rectangle in inches, but I would agree to just see that the GetBounds pageUnit parameter causes a change in the returned rectangle.

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(); 
+4
source share
2 answers

The information is a bit rare, I was able to find this MSDN forum post , which suggests that the bitmap has already been created, the units are already installed and do not change. Since GraphicsUnit is passed by reference, you look at it after the call, and you return to Pixel from Inch . If you really want to change the size by which the rectangle is drawn, set the Graphics.PageUnit Property to formGraphics to the GraphicsUnit that you want to draw the rectangle in.

Top Link:

In this example, the parameters of the Image.GetBounds method do not change the result, because the boundary of the bitmap was determined. The parameters only determine the length of the block in order to deal with a range, inch per inch, or dot by dot. But the parameters will not affect the result .

my accent

+4
source

I answered this question a little late, but I thought I would do it because I found it on Google, trying to answer the question “how many mm can I insert in my picture box?”, This would save me a lot of time, no need to understand , how to do it!. GetBounds is useless (if you want it in pixels ...), but you can find the relationship between drawing units and display pixels using the Graphics.TransformPoints method:

  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(); } 

This will display the main millimeter for displaying pixels (3.779527 in my case) - the world coordinates are 1 mm per pixel, this will change if you apply graphics.ScaleTransform.

Edit: Of course, this helps if you assign a bitmap to the pictureBox image property (and save the Graphics object to allow changes as needed).

+1
source

All Articles