I don’t understand a bit why this code does not work:

using (var ms = new MemoryStream()) { using (FileStream fs = new FileStream(file, FileMode.Open)) { fs.CopyTo(ms); } using (var bmp = (Bitmap)Image.FromStream(ms)) { bmp.SetResolution(300, 300); bmp.Save(@"E:\Projects\My Projects\ChangeDPI\test.png"); } } 

That is, at the DPI output, the image remains unchanged.

If you create a new Bitmap from bmp, then everything is saved successfully.

Is this some kind of library special, or am I doing something wrong?

  • It seems to be a bug: stackoverflow.com/q/19184265/276994 - VladD
  • @VladD, and in a more OS-independent manner can you work with pictures without resorting to other libs? For example, through WPF classes? It seems that in this framework they wanted to get away from GDI +. If OS bugs reluctantly rule, then they should support their framework better. - iluxa1810
  • Well, WPF seems to be also system-dependent, although less. But I did not try to change the resolution through WPF, I have to try. - VladD

1 answer 1

Try to replace

 bmp.SetResolution(300, 300); 

on

 bmp = new Bitmap(bmp, new Size(300, 300)); 

Most likely it will turn out and without resorting to third-party libs. Unsubscribe solved the problem.

  • Well, this is similar to the solution I found in the form of creating a new bmp based on bmp from a file. It seems that there are no other beautiful solutions. - iluxa1810