实际上,您必须区分屏幕上的图像尺寸和打印输出上的图像尺寸。
通常,您会找到以下公式:
inches = pixels / dpi
Run Code Online (Sandbox Code Playgroud)
所以如下:
pixel = inches * dpi
Run Code Online (Sandbox Code Playgroud)
实际上,这是用于打印的。
对于显示器,将 dpi 替换为 ppi,就可以了。
对于那些不熟悉英寸的人(像我一样):
inches = pixels / dpi
pixel = inches * dpi
1 centimeter = 0.393700787 inch
pixel = cm * 0.393700787 * dpi
Run Code Online (Sandbox Code Playgroud)
该例程将计算像素大小,以使图像在监视器上显示 X 厘米。
但在打印机上,您就没有那么容易了,因为您无法像获得 PPI 一样轻松获得 DPI(bmp.HorizontalResolution 和 bmp.VerticalResolution)。
public static int Cm2Pixel(double WidthInCm)
{
double HeightInCm = WidthInCm;
return Cm2Pixel(WidthInCm, HeightInCm).Width;
} // End Function Cm2Pixel
public static System.Drawing.Size Cm2Pixel(double WidthInCm, double HeightInCm)
{
float sngWidth = (float)WidthInCm; //cm
float sngHeight = (float)HeightInCm; //cm
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(1, 1))
{
sngWidth *= 0.393700787f * bmp.HorizontalResolution; // x-Axis pixel
sngHeight *= 0.393700787f * bmp.VerticalResolution; // y-Axis pixel
}
return new System.Drawing.Size((int)sngWidth, (int)sngHeight);
} // End Function Cm2Pixel
Run Code Online (Sandbox Code Playgroud)
用法如下:
public System.Drawing.Image Generate(string Text, int CodeSize)
{
int minSize = Cm2Pixel(2.5); // 100;
if (CodeSize < minSize)
CodeSize = minSize;
if (string.IsNullOrEmpty(Text))
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(CodeSize, CodeSize);
using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(bmp))
{
gfx.Clear(System.Drawing.Color.Black);
using(System.Drawing.Font fnt = new System.Drawing.Font("Verdana", 12, System.Drawing.FontStyle.Bold))
{
double y = CodeSize / 2.0 - fnt.Size;
gfx.DrawString("No Data", fnt, System.Drawing.Brushes.White, 5, (int)y, System.Drawing.StringFormat.GenericTypographic);
} // End Using fnt
} // End using gfx
return bmp;
} // End if (string.IsNullOrEmpty(Text))
...[Generate QR-Code]
return [Generated QR-Code]
}
Run Code Online (Sandbox Code Playgroud)