This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Change dpi

I´m working with TIFF and JPEG images. I have two options, change resolution or not.
The problem is that when I chage the ouput image resolution the size of the image is the same. The ResolutionModifier and ImageGenerator methods change the resolution but does not change the dimensions of the image.

using (var reader = TiffReader.Create(ficheroIN))
    using (var crop = new Crop(rectangulo))
    using (var writer = new TiffWriter(ficheroOUT))

    {
        var exif = new ExifDictionary();
        var iptc = new IptcDictionary();
        

        if (procesar_exif == false)
        {
            exif = reader.Exif;
            writer.Exif = exif;
        }
        else
        {
            if (checkBoxEXIF.Enabled)
                writer.Exif = InsertarEXIF(exif);
            if (checkBoxIPTC.Checked)
                writer.Iptc = InsertarIPTC(iptc);
            
        }

        if (border_size != 0)
        {
            using (var borderColor = RgbColor.Black)
            using (var generator = new ImageGenerator(crop.Rectangle.Width + border_size * 2, crop.Rectangle.Height + border_size * 2, reader.PixelFormat, borderColor))
            using (var blender = new Blender())
            {
                blender.TopImage = reader.Frames[0] + crop;
                blender.X = border_size;
                blender.Y = border_size;
                generator.DpiX = reader.DpiX;
                generator.DpiY = reader.DpiY;
                Pipeline.Run(generator + blender + writer);
                bitmap.Dispose();
            }
        }
        else
        {
            Pipeline.Run(reader + crop + writer);
        }
    }


Could you help me? If my input image after cropping is 21x15 cm. I should get the same size in 300 or 150 dpi.
Thanks in advance.
Hi,

It works as expected. You set a physical size of an image by a pair of values for pixel size and resolution. And very often there is a need to change a DPI preserving the pixel size. So, ResolutionModifier doesn't change pixels.

GM contains a helper class for unit conversion operation.

UnitConverter.ConvertUnitsToPixels(dpi150, 21, Unit.Cm);
UnitConverter.ConvertUnitsToPixels(dpi300, 21, Unit.Cm);


Use it to get the desired pixel size and add a resizer along with resolution modifier to the end of the TopImage pipeline, or just after the crop if the border is not needed.
Hi Eugene, thanks for your reply. I´ll tests and let you know..
Regards 😉
Eugene Kosmin;32571
Hi,

It works as expected. You set a physical size of an image by a pair of values for pixel size and resolution. And very often there is a need to change a DPI preserving the pixel size. So, ResolutionModifier doesn't change pixels.

GM contains a helper class for unit conversion operation.

UnitConverter.ConvertUnitsToPixels(dpi150, 21, Unit.Cm);
UnitConverter.ConvertUnitsToPixels(dpi300, 21, Unit.Cm);


Use it to get the desired pixel size and add a resizer along with resolution modifier to the end of the TopImage pipeline, or just after the crop if the border is not needed.


Hi Eugene. Finally I solved the problem. But I didn´t use UniConverter. I didn´t know how apply to my problem.
Attached code wich I used and it´s working for me. If anybody needs explanation let me know.
Regards and thanks again.

                if (border_size != 0)                                                                                   // TIENE BORDE 
                {
                    using (var borderColor = RgbColor.Black)
                    using (var generator = new ImageGenerator(0, 0, reader.PixelFormat, borderColor))
                    using (var blender = new Blender())
                    {
                        if (resolution == 0)                                                                            // MANTENEMOS RESOLUCION DE ENTRADA
                        {
                            blender.TopImage = reader.Frames[0] + crop;                                                 // COGEMOS LA IMAGEN DEL READER Y LA CORTAMOS
                            blender.X = (int)border_size;
                            blender.Y = (int)border_size;
                            generator.DpiX = reader.DpiX;
                            generator.DpiY = reader.DpiY;
                            generator.Width = crop.Rectangle.Width + (int)border_size * 2;
                            generator.Height = crop.Rectangle.Height + (int)border_size * 2;

                            Pipeline.Run(generator + blender + writer);
                        }
                        else                                                                                            // CAMBIAMOS LA RESOLUCION A LA SELECCIONADA
                        {
                            float ratio = resolution / reader.DpiX;                                                     // CALCULAMOS RATIO ENTRE DPI IN Y DPI OUT     
                            float border_size_tmp = border_size * ratio;                                                // APLICAMOS EL RATIO AL BORDE
                            border_size = (int)border_size_tmp;
                            float Pixels_X = ((rectangulo.Width / reader.DpiX) * resolution);                           //CALCULAMOS MEDIDAS PARA EL REESCALADO
                            float Pixels_Y = ((rectangulo.Height / reader.DpiY) * resolution);

                            generator.DpiX = resolution;
                            generator.DpiY = resolution;
                            generator.Width = (int)Pixels_X + (int)border_size * 2;
                            generator.Height = (int)Pixels_Y + (int)border_size * 2;

                            resize.Width = (int)Pixels_X ;
                            resize.Height = (int)Pixels_Y;
                            resize.InterpolationMode = ResizeInterpolationMode.Medium;

                            blender.TopImage = reader.Frames[0] + crop + resize;
                            blender.X = (generator.Width - resize.Width) / 2;
                            blender.Y = (generator.Height - resize.Height) / 2;
                            
                            Pipeline.Run(generator + blender + writer);
                        }

                        
                    }
                }
                else                    // NO TIENE BORDE
                {
                    if (resolution == 0) // MANTENEMOS LA RESOLUCION
                    {
                        Pipeline.Run(reader + crop + writer);
                    }
                    else               // CAMBIAMOS RESOLUCION
                    {
                        using (var generator = new ImageGenerator(0, 0, reader.PixelFormat, RgbColor.Black))
                        using (var blender = new Blender())

                        {
                            float Pixels_X = (rectangulo.Width / reader.DpiX) * resolution;
                            float Pixels_Y = (rectangulo.Height / reader.DpiY) * resolution;

                            resize.Width = (int)Pixels_X;
                            resize.Height = (int)Pixels_Y;
                            resize.InterpolationMode = ResizeInterpolationMode.Medium;

                            blender.TopImage = reader.Frames[0] + crop + resize;
                            blender.X = 0;
                            blender.Y = 0;
                            generator.DpiX = resolution;
                            generator.DpiY = resolution;
                            generator.Width = (int)Pixels_X;
                            generator.Height = (int)Pixels_Y;
                            Pipeline.Run(generator + blender + writer);
                        }
                    }

                }