Resize image in cm C #

I have a requirement that requests an image with 10 X 6.88 cm. I know that I can’t just convert from cm to pixels, because the size of one pixel depends on the user's screen resolution. I would like to know if there is a way to resize the image to have this size in cm. (I also need to save the image extension. For example: it is not possible to convert it to a PDF file or another extension)

+3
source share
5 answers

In fact, it depends on the resolution in which the user will print the image (dimensions in cm do not make much sense except what is printed). If the user wants to print, say, 200 dpi, then the image should be (10 / 2.54 * 200) at (6.88 / 2.54 * 200) pixels (for conversion between cm and inches, division with 2.54 is required). What resolution is required depends heavily on what image it represents 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 sample code on how to make the actual resize after you figure out the required image size, this SO answer should cover your needs.

+4

, .

:

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 .
, DPI , 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]
}
+3

, 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 6,88 .

+2

: DPI , ( ), / DPI, /...

0

: . ( cm). , (, , ).

, 3,93 "x 2,71". 393px x 271px, dpi 100x100. 39px x 27px, dpi 10x10.

, , , .:)

0

Source: https://habr.com/ru/post/1720888/


All Articles