题
我有一个要求,即要求一个图像与10X6,88l厘米。我知道,我不能简单的转换从厘米要素,原因之一像素的大小取决于用户显示决议。我想知道如果有一个方法可以调整的图像有那尺寸厘米。(我需要像延长。例如:不能转换成pdf或其他扩展)
解决方案
这实际上取决于用户打印图像的分辨率(以厘米为单位的尺寸除了打印时没有任何意义)。如果用户想要打印,比如200 dpi,则图像需要(10 / 2.54 * 200)乘以(6.88 / 2.54 * 200)像素(需要2.54的分割才能在cm和英寸之间进行转换) )。所需的分辨率在很大程度上取决于它是什么类型的图像,以及用户的质量要求。
所以只是说“我想通过Y cm调整到X”。没有意义。
有关如何在计算出所需大小的图像后如何进行实际调整大小的代码示例,这个SO答案应该满足您的需求。
其他提示
实际上,你有区别之间图像大小屏幕上,并将图像大小的打印输出。
通常,您找到公式:
inches = pixels / dpi
所以它下:
pixel = inches * dpi
这是印刷的,实际上。
为显示,替代dpi与私人参与基础设施,并有你。
对于那些(像我一样),是不熟悉英寸:
inches = pixels / dpi
pixel = inches * dpi
1 centimeter = 0.393700787 inch
pixel = cm * 0.393700787 * dpi
这种常规会计算素-大小有图像显示X-厘米上的监视器。
但是,在打印机,你没有那么容易,因为你不能让新闻部为容易,因为PPI(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厘米的范围。
Fredrik所说的话: 我会采用一个漂亮的DPI并要求图像分辨率更大(但宽高比相同),并在导出/打印图像时,将图像大小调整为其他程序/打印机使用的DPI ...
它可能就像这样简单:大多数图像存储每英寸像素数。计算出图像每个维度的像素数,并将其除以英寸数(从cm转换)。然后使用原始位,只需将字段修改为每英寸像素数(或更常见的是每英寸点数)。
所以你的照片必须是3.93“ x 2.71“。如果您的图像是393像素x 271像素,则将dpi设置为100x100。如果您的图像是39px x 27px,则可以将dpi设置为10x10。
虽然可能你需要做一些调整大小,正如其他答案所解释的那样。 :)