This project has moved. For the latest updates, please go here.

Increase DPI from 72 to 300 does not work

Jan 2 at 10:03 PM
What is the correct way to increase DPI from 72 to 300.
This is what I am trying.
                            Int32 density = 300;
            using (MagickImage resize = new MagickImage(folder + filename))
            {
                resize.Density = new Density(density, density, DensityUnit.PixelsPerInch);
                resize.VirtualPixelMethod = VirtualPixelMethod.Transparent;

                filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(filename);
                resize.Write(folder + filename);
            }
Coordinator
Jan 3 at 6:43 AM
Your code looks correct. Can you add a link to an input image that I can use for testing? And what is the issue that you are having?
Jan 24 at 3:38 PM
Sorry, I never received a notice that you replied.
Attached is my image.
TEXT

I tried the following code changes. STILL no luck.
            using (MagickImage resize = new MagickImage(folder + filename))
            {
                Single DPIChangeRatio = Convert.ToSingle(density) / Convert.ToSingle(res.X);
                resize.VirtualPixelMethod = VirtualPixelMethod.Transparent;
                resize.Resize(Convert.ToInt32(Convert.ToSingle(resize.Width) * DPIChangeRatio), Convert.ToInt32(Convert.ToSingle(resize.Height) * DPIChangeRatio));
                resize.Resample(density, density);
                resize.Density = new Density(density, density, DensityUnit.PixelsPerInch);

                filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(filename);
                resize.Write(folder + filename);
            }
Coordinator
Jan 25 at 7:35 AM
The GIF format does not support a DPI. You can set it but it is ignored by the GIF encoder.
Jan 25 at 2:34 PM
So how do I do it?

Should I first convert GIF to JPEG, would that help?

We don't care the format, we just want the density.
Jan 25 at 2:48 PM
I converted it to JPG, and save it.

I then edit the JPG copy, and I tried the code at the top of this post on that test image, and it had a huge white border. So I realize I must resize it.

I then I tried this code, on that test image. And it blurred. What am I missing here? Something in this process is blurring it.

using (MagickImage resize = new MagickImage(folder + filename))
        {
            Single DPIChangeRatio = Convert.ToSingle(density) / Convert.ToSingle(res.X);
            resize.VirtualPixelMethod = VirtualPixelMethod.Transparent;
            resize.Resize(Convert.ToInt32(Convert.ToSingle(resize.Width) * DPIChangeRatio), Convert.ToInt32(Convert.ToSingle(resize.Height) * DPIChangeRatio));
            resize.Resample(density, density);
            resize.Density = new Density(density, density, DensityUnit.PixelsPerInch);

            filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(filename);
            resize.Write(folder + filename);
        }
I also tried flipping the order of Resize and Resample, but it made no difference.
Coordinator
Jan 26 at 8:25 AM
Why do you need to resize and resample the image? This is probably what is messing up your image. Just setting the density should be enough.
Jan 27 at 1:28 AM
Requirements of the marketing department. The density must be 300 DPI.
The size must be 2.13 inches by 2.5 inches.

So how do I properly resize it, after changing the density, and not make it blurry.
Coordinator
Jan 27 at 11:36 AM
Edited Jan 27 at 11:49 AM
Maybe I should add a new overload for the Resample method:
using (MagickImage image = new MagickImage("yourfile.jpg"))
{
  Density density = new Density(300, DensityUnit.PixelsPerInch);
  double width = 2.13; // inches
  double height = 2.5; // inches
  image.Resample(density, width, height);
  /*
   * And this would do:
   * int imageWidth = 2.13 * 300; // 639
   * int imageHeight = 2.5 * 300; // 750
   * image.Density = 300;
   * image.Resize(imageWidth, imageHeight);
   */
}
The only issue with this is that the input image should have the correct dimensions. This means that you will need to do the calculation yourself anyways. You will need to do this because you either want to crop or extent the image to the required dimensions. So maybe this is a better solution:
  int dpi = 300;
  double width = 2.13; // inches
  double height = 2.5; // inches

  Density density = new Density(dpi, DensityUnit.PixelsPerInch);
  MagickGeometry geometry = density.ToGeometry(width, height);
  Console.WriteLine(geometry.ToString()); // 639x750
  // And you could use the geometry to resize the image.
Your thoughts?
Jan 27 at 2:37 PM
Based on your commends, I am assuming these are proposals, and not working solutions, correct?

Whichever is easier for you is great. My market department has been bitching at me about this for over a year, and I've spent countless hours on trying to get this to work and I simply cannot get it to work.

Let me know what is easiest for, and I will get it done on my end.
Coordinator
Jan 31 at 8:12 PM
Edited Jan 31 at 8:30 PM
I decided to add ToGeometry to the Density class. Below are some examples of how you could use this.
Density density = new Density(300, DensityUnit.PixelsPerCentimeter);
MagickGeometry geometry = density.ToGeometry(1.13, 1.5);

/* First option: image has the same aspect ratio */
using (MagickImage image = new MagickImage("logo:"))
{
  image.Density = density;

  image.Resize(geometry);

  image.Write("resize.png");
}

/* Second option: crop the image */
using (MagickImage image = new MagickImage("logo:"))
{
  image.Density = density;

  image.Crop(geometry, Gravity.Center);

  image.Write("crop.png");
}

/* Third option: extent the image */
using (MagickImage image = new MagickImage("logo:"))
{
  image.Density = density;

  image.Resize(geometry);
  image.Extent(geometry, Gravity.Center, MagickColors.Fuchsia);

  image.Write("extent.png");
}
Feb 1 at 4:31 AM
Thanks. Where would I get this? I do not see any new uploads since 1/15?
Coordinator
Feb 1 at 5:05 AM
You will need to wait till I publish the next release. Will try to do that this weekend.
Feb 13 at 10:04 PM
This is perfect. I have been fighting this for more than a year now. Thank you so much for putting in this override.