在C#中更改位图的分辨率

时间:2013-12-29 18:52:52

标签: c# graphics bitmap screenshot

我不想在C#中更改位图的分辨率并且已经找到了Bitmap.setResolution方法,但是这种方法实际上并没有改变物理分辨率。 然后我发现了这个:http://www.nullskull.com/q/10269424/change-the-resolution-of-png-image-and-save-it.aspx 但遗憾的是它没有用。 使用此代码,生成的图像只是原始图像的一部分。 这是我的代码:

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);

有没有办法让图片的分辨率更低,内容相同?

1 个答案:

答案 0 :(得分:0)

您刚刚使用Graphics.DrawImage的错误重载。请改用:

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);