Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Franchi_S5  
#1 Posted : Tuesday, March 20, 2018 9:13:56 AM(UTC)
Franchi_S5

Rank: Advanced Member

Groups: Member
Joined: 3/8/2018(UTC)
Posts: 33

Thanks: 15 times
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.

Code:
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.
Eugene Kosmin  
#2 Posted : Tuesday, March 20, 2018 4:15:31 PM(UTC)
Eugene Kosmin

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 9/19/2006(UTC)
Posts: 446

Was thanked: 24 time(s) in 24 post(s)
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.

Code:
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.
Best regards,
Eugene Kosmin.
Aurigma Development Team
thanks 1 user thanked Eugene Kosmin for this useful post.
Franchi_S5 on 3/21/2018(UTC)
Franchi_S5  
#3 Posted : Wednesday, March 21, 2018 2:24:34 AM(UTC)
Franchi_S5

Rank: Advanced Member

Groups: Member
Joined: 3/8/2018(UTC)
Posts: 33

Thanks: 15 times
Hi Eugene, thanks for your reply. I´ll tests and let you know..
Regards ;)
Franchi_S5  
#4 Posted : Friday, March 23, 2018 3:30:11 AM(UTC)
Franchi_S5

Rank: Advanced Member

Groups: Member
Joined: 3/8/2018(UTC)
Posts: 33

Thanks: 15 times
Originally Posted by: Eugene Kosmin Go to Quoted Post
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.

Code:
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.

Code:
                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);
                        }
                    }

                }
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.