0

I wan't to change the resolution of an Bitmap in C# and already found the Bitmap.setResolution method, but this method is not actually changing the physical resolution. Then i found this: http://www.nullskull.com/q/10269424/change-the-resolution-of-png-image-and-save-it.aspx But sadly it didn't work. With this code, the resulting image is just a part of the original one. Here is my code:

Bitmap b = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
                Graphics g = Graphics.FromImage(b);
                g.CopyFromScreen(0, 0, 0, 0, b.Size);

                Bitmap b2 = new Bitmap(100, 100);
                g = Graphics.FromImage(b2);
                g.DrawImage(b, new Rectangle(0, 0, b2.Width, b2.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel);

                b2.Save(Strings.common_documents + "tmp_screenshot.jpg", ImageFormat.Jpeg);

Is there a way of giving an Image a lower resolution with the same content?

user2422196
  • 297
  • 3
  • 13

1 Answers1

0

You were just using the wrong overload of Graphics.DrawImage. Use this instead:

string filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "image.png");

Bitmap b = new System.Drawing.Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(b);
g.CopyFromScreen(0, 0, 0, 0, b.Size);

Bitmap b2 = new Bitmap(500, 500);
g = Graphics.FromImage(b2);
g.DrawImage(b, new RectangleF(0, 0, b2.Width, b2.Height));

b2.Save(filename, ImageFormat.Jpeg);
System.Diagnostics.Process.Start(filename);
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • 1
    The other should work, too. It is used to also give the source rectangle, but giving the full size of the source is equivalent to not giving it at all. – PMF Dec 29 '13 at 20:44
  • @PMF: You are right. I ran it in a console app and may have been confused by the image of the console being caught blurry on app launch. The op's code seems fine. – Raheel Khan Dec 29 '13 at 20:49