简体   繁体   English

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

[英]Resizing an image in cm C#

I have an requirement that asks for an image with 10 X 6,88 cm. 我要求要求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. 我知道我不能简单地从cm转换为像素,导致一个像素大小取决于用户的显示分辨率。 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. eg: can't convert it to a pdf or other extension) (我还需要保留图片扩展名。例如:无法将其转换为pdf或其他扩展名)

It really depends on in which resolution the user will print the image (sizes in cm makes little sense other than when printed). 这实际上取决于用户打印图像的分辨率(以厘米为单位的尺寸除了打印时没有什么意义)。 If the user wants to make a print in, say 200 dpi, then the image would need to be (10 / 2.54 * 200) by (6.88 / 2.54 * 200) pixels (the division with 2.54 is needed to convert between cm and inches). 如果用户想要打印,比如200 dpi,则图像需要(10 / 2.54 * 200)乘以(6.88 / 2.54 * 200)像素(需要2.54的分割才能在cm和英寸之间进行转换) )。 Which resolution that is needed is highly dependent on what kind of image it is, and the quality requirements of the user. 所需的分辨率高度依赖于它是什么类型的图像,以及用户的质量要求。

So just saying "I want to resize to X by Y cm" does not really make sense. 所以只是说“我希望通过Y cm调整到X”并没有多大意义。

For a code sample on how to make the actual resize once you have figured out the needed size of the image, this SO answer should cover your needs. 有关如何在计算出所需大小的图像后如何进行实际调整大小的代码示例, 这个SO答案应该可以满足您的需求。

Actually, you have to differentiate between the images size on the screen, and the images size on the printout. 实际上,您必须区分屏幕上的图像大小和打印输出上的图像大小。

usually, you find the formula: 通常,你会找到公式:

inches = pixels / dpi

so it follows: 所以它遵循:

pixel = inches * dpi

This is for print, actually. 实际上,这是用于打印。
For the display, replace dpi with ppi, and there you are. 对于显示器,用ppi替换dpi,就在那里。

For those (like me) that are not familiar with inches: 对于那些不熟悉英寸的人(比如我):

inches = pixels / dpi
pixel = inches * dpi
1 centimeter = 0.393700787 inch
pixel = cm * 0.393700787  * dpi

This routine will calculate the pixel-size to have the image display X-cm on the monitor. 此例程将计算像素大小,使图像在监视器上显示X-cm。
But on the printer, you don't have it that easy, since you can't get the DPI as easy as the PPI (bmp.HorizontalResolution & bmp.VerticalResolution). 但是在打印机上,你没有那么容易,因为你不能像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

usage would go like this: 用法会是这样的:

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]
}

Image file formats like JPG and TIFF have an EXIF header which has information like horizontal and vertical DPI. 像JPG和TIFF这样的图像文件格式有一个EXIF标题 ,其中包含水平和垂直DPI等信息。

Thus if you get an image that has this metadata, you could verify the printable size. 因此,如果您获得具有此元数据的图像,则可以验证可打印的大小。

double DPC = Image_DPI * 0.393700787;

double widthInCm = Image_Width * DPC;
double heightInCm = Image_Height * DPC;

if (widthInCm <= 10 && heightInCm <= 6.88) // do stuff

If you need to resize images to never exceed these printable dimensions, you could do it the other way around, and calculate a DPI ratio that lets the image of dimensions W x H fit within 10cm x 6.88cm bounds. 如果您需要调整图像大小以不超过这些可打印尺寸,您可以反过来这样做,并计算DPI比率,使尺寸为W x H的图像适合10厘米x 6.88厘米的范围。

Fredrik所说的那种:我会选择一个漂亮的DPI,并要求图像分辨率更大(但宽高比相同),导出/打印图像时,将图像大小调整为其他程序使用的DPI /打印机...

It might be as simple as this: most images store the number of pixels per inch in them. 它可能就像这样简单:大多数图像存储每英寸像素数。 Figure out the number of pixels in each dimension of your image, and divide that by the number of inches (convert from cm). 计算出图像每个维度的像素数,并将其除以英寸数(从cm转换)。 Then use the original bits, just modify the field for the number of pixels per inch (or, more commonly, dots per inch). 然后使用原始位,只需将字段修改为每英寸像素数(或更常见的是每英寸点数)。

So your picture needs to be 3.93" x 2.71". 所以你的照片必须是3.93“x 2.71”。 If your image is 393px x 271px, you would set the dpi to 100x100. 如果您的图像是393像素x 271像素,则将dpi设置为100x100。 If your image is 39px x 27px, you would set the dpi to 10x10. 如果图像为39px x 27px,则将dpi设置为10x10。

Though probably you'll have to do some resizing, as explained by other answers. 虽然可能你不得不做一些调整大小,正如其他答案所解释的那样。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM