我有一个要求,要求图像与 10 X 6,88 厘米。我知道我不能简单地从厘米转换为像素,导致一个像素大小取决于用户显示分辨率。我想知道是否有一种方法来调整图像的大小,以厘米为单位。(我需要保持图像扩展。例如:不能将其转换为 pdf 或其他扩展名)
这实际上取决于用户将以哪种分辨率打印图像(以厘米为单位的大小除了打印时没有什么意义)。如果用户想要以 200 dpi 进行打印,则图像需要为(10 / 2.54 * 200)乘(6.88 / 2.54 * 200)像素(需要除以 2.54 才能在厘米和英寸之间转换)。所需的分辨率在很大程度上取决于用户对图像的要求以及质量。
所以只是说“我想通过 Y 厘米调整到 X”并没有真正意义。
有关如何在确定所需的图像大小后进行实际大小调整的代码示例,this SO answer应满足您的需求。
实际上,您必须区分屏幕上的图像大小和打印输出上的图像大小。
通常,你会发现公式:
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-cm 的图像。
但是在打印机上,您没有那么容易,因为您无法像 PPI (bmp.HorizontalResolution & amp;bmp.VerticalResolution) 那样容易地获得 DPI。
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 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 的图像适合 10cm x 6.88 cm 的范围。
Fredrik 所说的:我会采取一个很好的 DPI,并要求图像是该分辨率或更大(但纵横比相同),并在导出 / 打印图像时,将图像调整为其他程序 / 打印机使用的 DPI...
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(2条)