-3

I want to change the VerticalResolution and HorizontalResolution of a Bitmap to a fixed value of 300.

I have a Windows service that takes some TIFF and does some barcode related operations. Beside that at the end I create a multipage TIFF from single page ones.

The problem is that the original DPI are always 300 and the results have 96 DPI.

Even if resolution is the same and filesize is untouched (considering the additional pages) this seems the only relevant difference.

It is relevant because I need 300 DPI in every file.

This is the code I think the cause lies in, taken from here: https://www.codeproject.com/Articles/16904/Save-images-into-a-multi-page-TIFF-file-or-add-ima

private Bitmap ConvertToBitonal(Bitmap original)
    {
        Bitmap source = null;

        // If original bitmap is not already in 32 BPP, ARGB format, then convert
        if (original.PixelFormat != PixelFormat.Format32bppArgb)
        {
            source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
            source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
            using (Graphics g = Graphics.FromImage(source))
            {
                g.DrawImageUnscaled(original, 0, 0);
            }
        }
        else
        {
            source = original;
        }
  // some stuff here

  // Create destination bitmap
            Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);

  // other stuff
  }

Debugging it, I saw that before the instruction:

Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);

the bitmap had VerticalResolution 300 and HorizontalResolution 300. After it turns to 96x96.

How can I do change these Image Properties in order to have an Image with 300 DPI?


Solved using SetResolution method to set original Xdpi and Ydpi, default DPI for new Bitmap object are 96x96 as pointed out below in the answers.

refex
  • 173
  • 6
  • 10
  • 1
    Show us what you have tried by providing a [MCVE] and we can help you accordingly. – TheLethalCoder May 09 '17 at 10:44
  • We're not here to write code for you –  May 09 '17 at 10:53
  • I'm asking if there is a method to set a property i can't set directly. Not to write code for me. I will add more info – refex May 09 '17 at 10:54
  • 3
    Why are you trying to do this? Are you sure you [understand what this property actually does](http://stackoverflow.com/a/6221550/366904)? Anyway, you should be able to just call `SetResolution`. – Cody Gray - on strike May 09 '17 at 10:56
  • 1
    The original VerticalResolution and HorizontalResolution are set to 300, 300. My code scale the images to an unwanted 96x96. I want to fix that – refex May 09 '17 at 12:12
  • 1
    Please don't call code you just copied from somewhere "my code". You obviously have no idea what this code does and why it does what it does... – Piglet May 09 '17 at 13:07
  • 1
    I think ya'll were a bit harsh on reflex. I ran into a similar problem--thanks for asking the question reflex. – Jeff May 17 '19 at 14:18

1 Answers1

4

The code creates a Bitmap destination. A Bitmap's resolution defaults to 96dpi x 96 dpi. As no other resolution is set the output file has the default resolution...

The answer to your question can be found in the reference manual.

https://msdn.microsoft.com/en-us/library/system.drawing.bitmap(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.setresolution(v=vs.110).aspx

public void SetResolution(
        float xDpi,
        float yDpi
    )

Sets the resolution for this Bitmap.

If you would understand the code you copied from somewhere you would realize that you already had the answer to your question right in front of you...

source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • 1
    I eventually saw it. – refex May 09 '17 at 13:31
  • 1
    Since that function creates more than once a new Bitmap object and since the first time it reassigns the original DPI with SetResolution method, I assumed it would reassigns it even in a second occasion to preserve the original DPI. This code comes from a project with CPOL 1.02 license. What's wrong in using it in my project? Do you know every single line of every dependecies of your codes? Don't you never make assumptions on what someone else's code does? – refex May 09 '17 at 13:37
  • @refex No but I don't call it "my code" and I usually don't ask questions on stack overflow that could easily be answered by a) reading the 10 lines of code in front of me and/or b) by reading the reference manual of the class I want to change properties of. If you have code that does not do what you expect it to do, you can't just make assumptions. read it, try to understand each line. take notes... I mean how hard can it be to find that you set the resolution only for "source" but not for "destination" when your output has unexpected resolution? you don't need to be a programmer for that. – Piglet May 09 '17 at 13:59
  • 1
    You are right, just consider that I'm learning this language from few weeks and I never used this Class before, neither worked with image files so it take some time to understand where to look. Infact knowing that there is a SetResolution method helped me to debug the code (not 10 lines :) ) Thank you anyway – refex May 09 '17 at 14:09
  • 1
    @refex just learn to refer to reference documentations wherever you can. I neither know C# nor do I know that class or any of its functions. But I know how to get information. – Piglet May 09 '17 at 14:21