简体   繁体   中英

How draw rectangle in millimeter

using (var mem = new MemoryStream())
using (var bmp = new Bitmap(85, 54))
using (var gfx = Graphics.FromImage((Image)bmp))
{
    // gfx.SmoothingMode = SmoothingMode.AntiAlias;
    gfx.PageUnit = GraphicsUnit.Millimeter;
    gfx.FillRectangle(Brushes.Red, new Rectangle(0, 0, bmp.Width, bmp.Height));

    //add question
    gfx.DrawString(captcha, new Font("Arial", 5), Brushes.Blue, bmp.Width / 2, bmp.Height/2);

    //render as Jpeg
    bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
    img = this.File(mem.GetBuffer(), "image/Jpeg");
}

return img;

this not work. I need 85x54 millimeter

how do this?

I need draw for print

The size of this Bitmap is in pixels.

When you display a bitmap on a regular display a single pixel will be 1/96th of an inch. Other displays might have other DPI's ( Dots Per Inch ) - such as Retina displays

Most printers support at least 300 DPI.

So what you need to do is get the DPI of the screen or printer and size the bitmap accordingly or use a image format (vector?) that allows you to specify the DPI. Some bitmap formats also allow you to specify the intended DPI

Digital images are always in pixels. Never in millimeters or inches. Depending on the DPI (dots per inch) you'll use when printing, the pixels are translated to millimeters or inches.

For screen, use 72 pixels per inch, for print use 300.

For your picture (85x54mm = 3.34x2.12in) use (3.34 * 300) x (2.12 * 300) = 1002 x 637 pixels for print.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM