Set Image Resolution

May 22, 2015 at 8:15 AM
Edited May 22, 2015 at 8:15 AM
I have hundreds of images that I need to print in a specific paper size (40 X 30 centimeters). For that, I need to resize the images. The image resize method in ASP.NET works by supplying it with pixels, not centimeters. I am not that expert in pixels and photography. I converted 40 X 30 centimeters to 1512 X 1134 pixels. But, I have learned that to do such method, I need to set the resolution first, then specify the pixels that I need.

Therefore, and in my case, I need to set the image resolution first to 300 and then use 4724 X 3543 pixels to resize the images. This way will preserve the images quality and will result in all images having the same size.

So, I need to know how to set the image resolution to 300 in ASP.NET. I tried using the density method, but I could not get any result. Please advise.

May 22, 2015 at 5:56 PM
Edited May 22, 2015 at 5:56 PM
If you want to set the image resolution you should use the Density property of MagickImage. But it seems you already tried that. What do you mean by not getting any result?
May 22, 2015 at 10:56 PM
I used the following statement:

image.Density = new PointD(300);

The resolution of the original image was 180 dpi. However, the new image was 1 dpi only which makes no sense. Please advise.

May 23, 2015 at 7:48 AM
What is the format of your output image and how do you know the density is only 1 dpi?
May 23, 2015 at 9:56 AM
Thank you for your follow up. Now, things started getting more clear to me. I have done more testing and I realized that there is no issue with Density property. Density property works fine and it sets the resolution to 300 dpi as I require. I can check this by right clicking on the output image and then click on properties. All information about the image are listed there. However, what confused me is that appending an image (image) to a newly created image (footer_image) will set the resolution of the output image (output) to 1 dpi. I can also fix this by setting the resolution of the new image (footer_image) to 300. This will ensure that the output image has the correct resolution. Now, only one thing is remaining. Though the resolution of the output is now correct, the output image size dropped from 10.4 MB to 3.70 MB. If no image appending is involved (only resolution is changed), the image size drops by only 0.2 MB. But once I append the image to the footer image, the size drops drastically.

Note that I am using .JPG format for both input and output.


MagickImage image = new MagickImage("#PATH#");
MagickImageCollection images = new MagickImageCollection();
MagickImage footer_image = new MagickImage();
footer_image.BackgroundColor = Color.Black;
footer_image.FillColor = new MagickColor(Color.White);
footer_image.FontPointsize = 100;
footer_image.TextDirection = TextDirection.RightToLeft;
footer_image.Font = "Arial";
footer_image.Read("pango:<span foreground=\"white\">" + "مرحبا" + "</span>");
footer_image.Extent(image.Width, 300, Gravity.East);
footer_image.Density = new PointD(300);
output = images.AppendVertically();
May 23, 2015 at 4:23 PM
Can you add a link to the image you are using on something like dropbox or onedrive? If you don't want to publicly share your image then feel free to contact me through CodePlex. And I would advise you to just set the density on the 'output' variable. That is the image that gets written to disk. And does the file name from 'physicalNewImagePath' include a .jpg extension?