I have an requirement that asks for an image with 10 X 6,88 cm. I know that I can't simple convert from cm to pixels, cause one pixel size depends on the user display resolution. I would like to know if there is a way to resize an image to have that size in cm. (I need to keep the image extension also. e.g.: can't convert it to a pdf or other extension)
5 回答
这实际上取决于用户将以何种分辨率打印图像(以厘米为单位的尺寸除了打印时几乎没有意义)。如果用户想要以 200 dpi 进行打印,则图像需要是 (10 / 2.54 * 200) x (6.88 / 2.54 * 200) 像素(需要 2.54 的除法才能在厘米和英寸之间进行转换)。需要哪种分辨率很大程度上取决于它是什么类型的图像,以及用户的质量要求。
所以只是说“我想将 X 调整为 Y cm”并没有真正的意义。
有关如何在确定所需的图像大小后进行实际调整大小的代码示例,此 SO 答案应该可以满足您的需求。
实际上,您必须区分屏幕上的图像尺寸和打印输出上的图像尺寸。
通常,您会找到以下公式:
inches = pixels / dpi
所以它如下:
pixel = inches * dpi
实际上,这是用于打印的。
对于显示,将 dpi 替换为 ppi,就可以了。
对于那些(像我一样)不熟悉英寸的人:
inches = pixels / dpi
pixel = inches * dpi
1 centimeter = 0.393700787 inch
pixel = cm * 0.393700787 * dpi
此例程将计算像素大小以使图像在监视器上显示 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
用法会是这样的:
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]
}
JPG 和 TIFF 等图像文件格式具有EXIF 标头,其中包含水平和垂直 DPI 等信息。
因此,如果您获得具有此元数据的图像,则可以验证可打印尺寸。
double DPC = Image_DPI * 0.393700787;
double widthInCm = Image_Width * DPC;
double heightInCm = Image_Height * DPC;
if (widthInCm <= 10 && heightInCm <= 6.88) // do stuff
如果您需要调整图像的大小以永远不超过这些可打印尺寸,您可以反过来做,并计算一个 DPI 比率,使尺寸 W x H 的图像适合 10 厘米 x 6.88 厘米的范围。
弗雷德里克所说的那种:我会采用一个不错的 DPI,并要求图像具有该分辨率或更大(但纵横比相同),并且在导出/打印图像时,将图像大小调整为其他程序使用的 DPI /打印机...
可能就这么简单:大多数图像都存储了每英寸的像素数。计算出图像每个维度的像素数,然后除以英寸数(从厘米转换)。然后使用原始位,只需修改每英寸像素数(或更常见的是每英寸点数)的字段。
所以你的图片需要是 3.93" x 2.71"。如果您的图像是 393 像素 x 271 像素,您可以将 dpi 设置为 100x100。如果您的图像是 39 像素 x 27 像素,您可以将 dpi 设置为 10x10。
正如其他答案所解释的那样,尽管您可能必须进行一些调整大小。:)