【问题标题】:Changing the resolution of an Bitmap in C# [duplicate]在 C# 中更改位图的分辨率 [重复]
【发布时间】:2014-01-16 16:57:11
【问题描述】:

我不想在 C# 中更改 Bitmap 的分辨率,并且已经找到了 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);

有没有办法在相同内容的情况下为图像提供较低的分辨率?

【问题讨论】:

  • 我看不到明显的错误。您的代码应该可以正常工作...

标签: c# graphics bitmap screenshot


【解决方案1】:

您只是使用了错误的 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);

【讨论】:

  • 另一个也应该可以。它也用于给出源矩形,但给出源的全尺寸相当于根本不给出。
  • @PMF:你是对的。我在控制台应用程序中运行它,并且可能对应用程序启动时控制台图像模糊不清感到困惑。操作的代码看起来不错。
猜你喜欢
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多