将 Win Form 上的区域保存为 BMP 或 Jpeg
Save area on a Win Form as BMP or Jpeg
我正在尝试在 win 窗体上绘制一个矩形,并保存选定的矩形
作为磁盘上的图像(bmp 或 jpeg)。但我对保存部分感到震惊。目前我可以画矩形
并将矩形作为 mRect
变量
我通过 google 搜索和各种文章进行了努力,但没有成功。
我当前的表单事件代码是:
Point selPoint;
Rectangle mRect;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
selPoint = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point p = e.Location;
int x = Math.Min(selPoint.X, p.X);
int y = Math.Min(selPoint.Y, p.Y);
int w = Math.Abs(p.X - selPoint.X);
int h = Math.Abs(p.Y - selPoint.Y);
mRect = new Rectangle(x, y, w, h);
this.Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Blue, mRect);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// ???
}
在您的表单中使用 RectangleToScreen() 将您的选择矩形从客户端坐标转换为屏幕坐标:
Rectangle screenRC = this.RectangleToScreen(mRect);
Bitmap bmp = new Bitmap(screenRC.Width, screenRC.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(screenRC.Left, screenRC.Top, 0, 0, bmp.Size);
}
bmp.Save("a1.bmp");
我正在尝试在 win 窗体上绘制一个矩形,并保存选定的矩形
作为磁盘上的图像(bmp 或 jpeg)。但我对保存部分感到震惊。目前我可以画矩形
并将矩形作为 mRect
变量
我通过 google 搜索和各种文章进行了努力,但没有成功。 我当前的表单事件代码是:
Point selPoint;
Rectangle mRect;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
selPoint = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point p = e.Location;
int x = Math.Min(selPoint.X, p.X);
int y = Math.Min(selPoint.Y, p.Y);
int w = Math.Abs(p.X - selPoint.X);
int h = Math.Abs(p.Y - selPoint.Y);
mRect = new Rectangle(x, y, w, h);
this.Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Blue, mRect);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// ???
}
在您的表单中使用 RectangleToScreen() 将您的选择矩形从客户端坐标转换为屏幕坐标:
Rectangle screenRC = this.RectangleToScreen(mRect);
Bitmap bmp = new Bitmap(screenRC.Width, screenRC.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(screenRC.Left, screenRC.Top, 0, 0, bmp.Size);
}
bmp.Save("a1.bmp");