从图形调整图像大小?

soo*_*ise 4 c# graphics

我试图在从屏幕上复制它后调整图像大小,并且无法弄清楚如何做到这一点.我一直在阅读的教程建议使用Graphics.DrawImage来调整图像大小,但是当我运行此代码时,它不会调整大小.

Bitmap b = new Bitmap(control.Width, control.Height);
Graphics g = Graphics.FromImage(b);
g.CopyFromScreen(control.Parent.RectangleToScreen(control.Bounds).X, control.Parent.RectangleToScreen(control.Bounds).Y, 0, 0, new Size(control.Bounds.Width, control.Bounds.Height), CopyPixelOperation.SourceCopy);

g.DrawImage(b, 0,0,newWidth, newHeight);
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,谢谢!

Jam*_*rgy 6

试试这个.使用DrawImage时,图形不会"替换"图像 - 它会在源上绘制输入图像,这与您尝试绘制的图像相同.

可能是一个更简洁的方式来做到这一点,但.....

Bitmap b = new Bitmap(control.Width, control.Height);
using (Graphics g = Graphics.FromImage(b)) {
   g.CopyFromScreen(control.Parent.RectangleToScreen(control.Bounds).X, 
      control.Parent.RectangleToScreen(control.Bounds).Y, 0, 0, 
      new Size(control.Bounds.Width, control.Bounds.Height),
      CopyPixelOperation.SourceCopy);
}
Bitmap output = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(output)) {
  g.DrawImage(b, 0,0,newWidth, newHeight);
}
Run Code Online (Sandbox Code Playgroud)