我不想在 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);
有没有办法在相同内容下为图像提供较低的分辨率?
你只是使用了错误的重载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);