0
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

Mediator
  • 14,951
  • 35
  • 113
  • 191
  • 2
    Of courser it not works - pixels and millimeters aren't the same thing. – Renatas M. Aug 31 '12 at 07:39
  • Try to use Graphics class. It has PageUnit property which you can set to millimeters. Or calculate how many pixels fit in 1mm as others stated. – Renatas M. Aug 31 '12 at 07:45

2 Answers2

1

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

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115
  • Except of course it isn't really always going to be 1/96th of an inch. For example, you can run the same monitor at 1024x768 or 1920x1200 resolution - clearly the "pixels" are going to be quite different in size when you do that! – Matthew Watson Aug 31 '12 at 08:05
  • @MatthewWatson - Correct I was just stating the regular case. – Emond Aug 31 '12 at 08:24
0

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.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195