Resizing an image in cm C#

北慕城南 提交于 2019-12-10 14:07:37

问题


I have an requirement that asks for an image with 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. 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. e.g.: can't convert it to a pdf or other extension)


回答1:


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). 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.

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.




回答2:


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.

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.
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).

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



回答3:


Image file formats like JPG and TIFF have an EXIF header which has information like horizontal and vertical 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.




回答4:


Kind of what Fredrik is saying: I would take a nice DPI and require the image to be that resolution or bigger (but is the same aspect ratio) and when exporting/printing the image, resize the image to the DPI used by the other program/printer...




回答5:


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). 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". If your image is 393px x 271px, you would set the dpi to 100x100. If your image is 39px x 27px, you would set the dpi to 10x10.

Though probably you'll have to do some resizing, as explained by other answers. :)



来源:https://stackoverflow.com/questions/1613117/resizing-an-image-in-cm-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!