我正在嘗試在 win 表單上繪制一個矩形,并將選定的矩形保存為磁盤上的影像(bmp 或 jpeg)。但我對保存部分感到震驚。目前我可以繪制矩形并給我矩形作為mRect變數
我通過谷歌搜索和各種文章努力嘗試,但徒勞無功。我當前的表單事件代碼是:
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)
{
// ???
}
uj5u.com熱心網友回復:
使用RectangleToScreen()與您的 Form 將您的選擇矩形從客戶端坐標轉換為螢屏坐標:
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");
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367539.html
上一篇:任何想法如何解決快速影片背景圖片
