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.