这是应一个网友的问题而做的一个小程序,可以保存为位图,我觉得还是有一定的参考价值的,所以就把相关代码贴了出来 。
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace HandImage
{
public partial class frmTest : Form
{
List < Points > HandTrack = new List < Points > ();
public Graphics pb_Graphics;
Bitmap pb_BMP = new Bitmap( 239 , 235 );
int index = 0 ; // 第几笔
public frmTest()
{
InitializeComponent();
}
private void pnlBar_MouseMove( object sender, MouseEventArgs e)
{
HandTrack[index].PT.Add( new Point(e.X, e.Y));
pnlBar.Refresh();
}
private void pnlBar_Paint( object sender, PaintEventArgs e)
{
pb_Graphics = Graphics.FromImage((System.Drawing.Image)pb_BMP);
pb_Graphics.Clear(pnlBar.BackColor);
foreach (Points item in HandTrack)
{
pb_Graphics.DrawLines( new Pen(Color.Black), item.PT.ToArray());
}
e.Graphics.DrawImage(pb_BMP, 0 , 0 );
pb_Graphics.Dispose();
}
class Points
{
public List < Point > PT = new List < Point > ();
}
private void btnClear_Click( object sender, EventArgs e)
{
index = 0 ;
HandTrack = new List < Points > ();
pnlBar.Refresh();
}
private void pnlBar_MouseUp( object sender, MouseEventArgs e)
{
index ++ ;
}
private void pnlBar_MouseDown( object sender, MouseEventArgs e)
{
HandTrack.Add( new Points());
HandTrack[index].PT.Add( new Point(e.X, e.Y));
pnlBar.Refresh();
}
private void btnSave_Click( object sender, EventArgs e)
{
dlgSaveFile.Filter = " 位图(*.bmp)|*.bmp " ;
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
string strFile = dlgSaveFile.FileName;
pb_BMP.Save(strFile, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace HandImage
{
public partial class frmTest : Form
{
List < Points > HandTrack = new List < Points > ();
public Graphics pb_Graphics;
Bitmap pb_BMP = new Bitmap( 239 , 235 );
int index = 0 ; // 第几笔
public frmTest()
{
InitializeComponent();
}
private void pnlBar_MouseMove( object sender, MouseEventArgs e)
{
HandTrack[index].PT.Add( new Point(e.X, e.Y));
pnlBar.Refresh();
}
private void pnlBar_Paint( object sender, PaintEventArgs e)
{
pb_Graphics = Graphics.FromImage((System.Drawing.Image)pb_BMP);
pb_Graphics.Clear(pnlBar.BackColor);
foreach (Points item in HandTrack)
{
pb_Graphics.DrawLines( new Pen(Color.Black), item.PT.ToArray());
}
e.Graphics.DrawImage(pb_BMP, 0 , 0 );
pb_Graphics.Dispose();
}
class Points
{
public List < Point > PT = new List < Point > ();
}
private void btnClear_Click( object sender, EventArgs e)
{
index = 0 ;
HandTrack = new List < Points > ();
pnlBar.Refresh();
}
private void pnlBar_MouseUp( object sender, MouseEventArgs e)
{
index ++ ;
}
private void pnlBar_MouseDown( object sender, MouseEventArgs e)
{
HandTrack.Add( new Points());
HandTrack[index].PT.Add( new Point(e.X, e.Y));
pnlBar.Refresh();
}
private void btnSave_Click( object sender, EventArgs e)
{
dlgSaveFile.Filter = " 位图(*.bmp)|*.bmp " ;
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
string strFile = dlgSaveFile.FileName;
pb_BMP.Save(strFile, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}
}