C# 如何绘制多个矩形并将其保存到加载的.jpgc
我试图在Windows窗体应用程序中打开一个.jpg,在它上面画一个矩形,并用矩形保存它。 在下面的代码中,我可以加载一个.jpg并在pictureBox中绘制一个矩形,但不能将.jpg与该矩形一起保存。有人知道这个问题吗C# 如何绘制多个矩形并将其保存到加载的.jpgc,c#,save,C#,Save,我试图在Windows窗体应用程序中打开一个.jpg,在它上面画一个矩形,并用矩形保存它。 在下面的代码中,我可以加载一个.jpg并在pictureBox中绘制一个矩形,但不能将.jpg与该矩形一起保存。有人知道这个问题吗 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EditScreen
{
public partial class Form1 : Form
{
Rectangle mRect;
Image mainimage;
Bitmap newBitmap;
Graphics g;
Boolean opened = false;
OpenFileDialog ofd = new OpenFileDialog();
SaveFileDialog sfd = new SaveFileDialog();
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
mainimage = Image.FromFile(ofd.FileName);
newBitmap = new Bitmap(ofd.FileName);
pictureBox1.Image = mainimage;
opened = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult dr = sfd.ShowDialog();
if (opened)
{
mainimage.Save(sfd.FileName);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
pictureBox1.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
Graphics g = e.Graphics;
g.DrawRectangle(pen, mRect);
}
}
}
}
我听说我必须在位图上绘制,但当我这样做时,我不知道如何使用PaintEventArgs。你能给我一个例子,让我的代码和图纸一起保存吗 ,它是用来画线的,但获取图形对象的关键概念是您所需要的。然后你可以使用这个方法你好,谢谢你的快速回答。但是如果我像你给出的这个例子那样做,我不能用我的鼠标画画,我怎么能用鼠标画画呢?在本例中,它在启动应用程序后直接生成矩形。我给您的示例是您应该在Save函数中使用的。您仍然应该保留绘制代码,以便在用户交互时绘制。当我像您的示例那样保存时,它只保存加载的图片而不保存图形,您可以制作一个代码片段给我看吗?在给定的示例中,当我这样做时,它可以工作,但在该示例中,我无法使用鼠标绘制。