picturebox画图, 已经实现,求如何保存图片 那你就在按钮里重新执行Graphics g = pictureBox1.CreateGraphics();并把它画到一个新建Image里,然后save 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Graphics g = pictureBox1.CreateGraphics();//Graphics g = Graphics.FromImage(this.pictureBox1.Image);被你注释掉的那句不就可以么,不要创建 Graphics另外 Graphics 的 Dispose 不要忘了 不要在MouseMove里面CreateGraphics,它会导致GDI资源泄露,一定时间后可能会导致程序崩溃等。用this.pictureBox1.Invalidate来强迫它更新。用数据结构(那些currentScribble ,scribbles)等来保存自定义标志,并在更新,或存图的时候重画。public partial class Form1 : Form{ public Form1() { InitializeComponent(); this.pictureBox1.Paint += pictureBox1_Paint; // 可以移动到设计器生成代码里 } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { currentScribble = new List<Point>(); currentScribble.Add(e.Location); } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && this.currentScribble != null) { currentScribble.Add(e.Location); RedrawPictureBox(); } } List<Point> currentScribble = null; List<Point[]> scribbles = new List<Point[]>(); void pictureBox1_Paint(object sender, PaintEventArgs e) { using (Pen pen = new Pen(Color.Blue, 2)) { foreach (Point[] points in scribbles) { e.Graphics.DrawLines(pen, points); } if (this.currentScribble != null) { e.Graphics.DrawLines(pen, this.currentScribble.ToArray()); } } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (currentScribble != null && currentScribble.Count > 1) { RedrawPictureBox(); this.scribbles.Add(currentScribble.ToArray()); } this.currentScribble = null; } private void RedrawPictureBox() { if (currentScribble.Count >= 2) { Rectangle last = new Rectangle(currentScribble[currentScribble.Count - 2], Size.Empty); Rectangle current = new Rectangle(currentScribble[currentScribble.Count - 1], Size.Empty); Rectangle invalidateArea = Rectangle.Inflate(Rectangle.Union(last, current), 2, 2); this.pictureBox1.Invalidate(invalidateArea); } } private void button5_Click(object sender, EventArgs e) { Rectangle rect = new Rectangle(Point.Empty, this.pictureBox1.Size); using (Bitmap bmp = new Bitmap(rect.Width, rect.Height)) { this.pictureBox1.DrawToBitmap(bmp, rect); // 画pictureBox1显示的图,假定它没有边框 using (Graphics g = Graphics.FromImage(bmp)) { pictureBox1_Paint(this, new PaintEventArgs(g, rect)); // 画自定义标记 } bmp.Save(@"D:\2.jpg", ImageFormat.Jpeg); } MessageBox.Show("ok"); }} 不显示删除回复显示所有回复显示星级回复显示得分回复 WF持久化问题,触发结束工作流处理事件报错。 关于用c#实现语音识别的问题!!! C#实现打印机? 初级入门的C#代码问题 求助!log4net写入oracle问题 如何根据进程名取pid c# 为什么以C#的指针停止不前? 关于编写代理服务器的问题 C# 序列化性能优化 求助:Response.writefile为什么只能输出10m以下的文件啊? c#创建桌面快捷方式 npoi 生成Excel文档的一个问题
//Graphics g = Graphics.FromImage(this.pictureBox1.Image);被你注释掉的那句不就可以么,不要创建 Graphics
另外 Graphics 的 Dispose 不要忘了
用this.pictureBox1.Invalidate来强迫它更新。
用数据结构(那些currentScribble ,scribbles)等来保存自定义标志,并在更新,或存图的时候重画。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.pictureBox1.Paint += pictureBox1_Paint; // 可以移动到设计器生成代码里
} private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
currentScribble = new List<Point>();
currentScribble.Add(e.Location);
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && this.currentScribble != null)
{
currentScribble.Add(e.Location);
RedrawPictureBox();
}
} List<Point> currentScribble = null;
List<Point[]> scribbles = new List<Point[]>();
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Blue, 2))
{
foreach (Point[] points in scribbles)
{
e.Graphics.DrawLines(pen, points);
}
if (this.currentScribble != null)
{
e.Graphics.DrawLines(pen, this.currentScribble.ToArray());
}
}
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (currentScribble != null && currentScribble.Count > 1)
{
RedrawPictureBox();
this.scribbles.Add(currentScribble.ToArray());
}
this.currentScribble = null;
} private void RedrawPictureBox()
{
if (currentScribble.Count >= 2)
{
Rectangle last = new Rectangle(currentScribble[currentScribble.Count - 2], Size.Empty);
Rectangle current = new Rectangle(currentScribble[currentScribble.Count - 1], Size.Empty);
Rectangle invalidateArea = Rectangle.Inflate(Rectangle.Union(last, current), 2, 2);
this.pictureBox1.Invalidate(invalidateArea);
}
} private void button5_Click(object sender, EventArgs e)
{
Rectangle rect = new Rectangle(Point.Empty, this.pictureBox1.Size);
using (Bitmap bmp = new Bitmap(rect.Width, rect.Height))
{
this.pictureBox1.DrawToBitmap(bmp, rect); // 画pictureBox1显示的图,假定它没有边框
using (Graphics g = Graphics.FromImage(bmp))
{
pictureBox1_Paint(this, new PaintEventArgs(g, rect)); // 画自定义标记
}
bmp.Save(@"D:\2.jpg", ImageFormat.Jpeg);
}
MessageBox.Show("ok");
}
}