none
Correcting image's DPI settings RRS feed

  • Question

  • I have an application where I allow the user to import various types of images.  When they select an image, I take it and move it into a TIFF format, with compression and multi-page [if necessary].  I've come across this odd tiff image that came from a Cisco fax software where the vertical and horizontal resolutions differ....

     

    The image is 1728 x 1072 and the resolution is 204 dpi x 98 dpi [basically an 8.5" x 11" document].  I am trying to take this image and convert it to 200 DPI.

     

    My code is pretty simple and I assume that I am just doing something incredibly stupid:

     

     

    Code Snippet

    private static Bitmap ConvertToRGB(Bitmap OriginalBitmap, int dpi)

    {

    float originalWidth = OriginalBitmap.Width / OriginalBitmap.HorizontalResolution;

    float originalHeight = OriginalBitmap.Height / OriginalBitmap.VerticalResolution;

    float newWidth = originalWidth * dpi;

    float newHeight = originalHeight * dpi;

    Bitmap NewImage = new Bitmap((int)Math.Round(newWidth,0), (int)Math.Round(newHeight,0), PixelFormat.Format32bppArgb);

    NewImage.SetResolution(dpi, dpi);

    using (Graphics g = Graphics.FromImage(NewImage))

    {

    g.DrawImage(OriginalBitmap, 0,0,newWidth, newHeight);

    }

    return NewImage;

    }

     

     

    What ends up happening with the image I describe above is an image which is not tall enough.  When I look at the resulting image, it seems like "DrawImage" is using the horizontal resolution when drawing vertically [it ends up being the right width but the height is about 1/2 of what it should be].  In the code above, the "dpi" parameter is the NEW dpi that I want.

     

     

    Tuesday, February 12, 2008 7:18 PM

Answers

  • Zor_Prime,

    I cannot confirm your results. Running a test with the code you posted resulted in an image with the correct size. The only changes I made were to load the bitmap from disk and save the result to another file, and finally I used small color TIFF's. None of this changes seem significant.

     

    In one of my tests the original vertical size was 216 pixels @ 98dpi and the result was 441 pixels @ 200dpi, which seems correct.

     

    Now: either something happens to the image after it is processed by your code, or you might be viewing the images with an application that does not handle the dpi settings correctly (notably Corel PhotoPaint). At this point I would try to save the original and the new images to disk and try to compare the values.

     

    HTH

    --mc

    Wednesday, February 13, 2008 2:45 AM

All replies

  • Zor_Prime,

    I cannot confirm your results. Running a test with the code you posted resulted in an image with the correct size. The only changes I made were to load the bitmap from disk and save the result to another file, and finally I used small color TIFF's. None of this changes seem significant.

     

    In one of my tests the original vertical size was 216 pixels @ 98dpi and the result was 441 pixels @ 200dpi, which seems correct.

     

    Now: either something happens to the image after it is processed by your code, or you might be viewing the images with an application that does not handle the dpi settings correctly (notably Corel PhotoPaint). At this point I would try to save the original and the new images to disk and try to compare the values.

     

    HTH

    --mc

    Wednesday, February 13, 2008 2:45 AM
  • On the image that you processed, was the horizontal DPI different than the vertical DPI?  My code works fine when the DPIs are the same but seems to have issues when they differ.
    Wednesday, February 13, 2008 4:11 AM
  • It ends up that it was actually converting it but the image which was being shown was the pre-converted image!  I forgot that I added in some image caching to speed up the re-display of the image -- the pre-converted image was being cached but it wasn't clearing the image cache when it converted the image to the right format.

     

     

    Wednesday, February 13, 2008 6:05 PM