以cm C为单位调整图像大小#

我要求要求10 X 6,88 cm的图像。 我知道我不能简单地从cm转换为像素,导致一个像素大小取决于用户的显示分辨率。 我想知道是否有办法调整图像大小以厘米为单位。 (我还需要保留图片扩展名。例如:无法将其转换为pdf或其他扩展名)

这实际上取决于用户打印图像的分辨率(以厘米为单位的尺寸除了打印时没有什么意义)。 如果用户想要打印,比如200 dpi,则图像需要(10 / 2.54 * 200)乘以(6.88 / 2.54 * 200)像素(需要2.54的分割才能在cm和英寸之间进行转换) )。 所需的分辨率高度依赖于它是什么类型的图像,以及用户的质量要求。

所以只是说“我希望通过Y cmresize”并没有多大意义。

有关如何在确定所需大小的图像后如何进行实际resize的代码示例, 此SO答案应该可以满足您的需求。

实际上,您必须区分屏幕上的图像大小和打印输出上的图像大小。

通常,你会找到公式:

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.Horizo​​ntalResolution&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等信息。

因此,如果您获得具有此元数据的图像,则可以validation可打印的大小。

 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设置为100×100。 如果您的图像是39px x 27px,则将dpi设置为10×10。

虽然可能你需要做一些resize,正如其他答案所解释的那样。 🙂