我要求提供10 X 6,88 cm的图像。 我知道我不能简单地从cm转换为像素,导致一个像素大小取决于用户的显示分辨率。 我想知道是否有办法调整图像大小以厘米为单位。 (我还需要保留图片扩展名。例如:无法将其转换为pdf或其他扩展名)
答案 0 :(得分:4)
这实际上取决于用户打印图像的分辨率(以厘米为单位的尺寸除了打印时没有什么意义)。如果用户想要打印,比如200 dpi,则图像需要(10 / 2.54 * 200)乘以(6.88 / 2.54 * 200)像素(需要2.54的分割才能在cm和英寸之间进行转换) )。所需的分辨率在很大程度上取决于它是什么类型的图像,以及用户的质量要求。
所以只是说“我希望通过Y cm调整大小”并不合理。
有关如何在计算出所需尺寸的图片后如何进行实际调整大小的代码示例,this SO answer应该可以满足您的需求。
答案 1 :(得分:4)
实际上,您必须区分屏幕上的图像尺寸和打印输出上的图像尺寸。
通常,你会找到公式:inches = pixels / dpi
所以它如下:
pixel = inches * dpi
实际上,这是用于打印的。
对于显示器,用ppi替换dpi,就在那里。
对于那些不熟悉英寸的人(比如我):
inches = pixels / dpi
pixel = inches * dpi
1 centimeter = 0.393700787 inch
pixel = cm * 0.393700787 * dpi
此例程将计算像素大小,使图像在显示器上显示X-cm 但是在打印机上,你没有那么容易,因为你不能像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]
}
答案 2 :(得分:2)
像JPG和TIFF这样的图像文件格式有EXIF header,其中包含水平和垂直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厘米的范围。
答案 3 :(得分:0)
Fredrik所说的话: 我会采用一个漂亮的DPI并要求图像分辨率更高或者更大(但宽高比相同),并且在导出/打印图像时,将图像大小调整为其他程序/打印机使用的DPI ...
答案 4 :(得分:0)
它可能就像这样简单:大多数图像存储每英寸像素数。计算出图像每个维度的像素数,并将其除以英寸数(从cm转换)。然后使用原始位,只需将字段修改为每英寸像素数(或者更常见的是每英寸点数)。
所以你的照片必须是3.93“x 2.71”。如果您的图像是393像素x 271像素,则将dpi设置为100x100。如果您的图像是39px x 27px,则可以将dpi设置为10x10。
虽然可能你需要做一些调整大小,正如其他答案所解释的那样。 :)