今天做一个在windows mobile中画图,并保存到模拟器文件夹下的例子。ok新建一个项目,添加一个窗体。界面设计如下,在屏幕的中间我放了一个图片框。
要画图,肯定要用到鼠标事件,具体用到哪些呢,应该是Mouse_Down,Mouse_Move,Mouse_Up。首先当鼠标按下时,我们创建一个点的list数组。当鼠标移动的时候我们就往这个list数组里面添加点。在高中我们就学过点成线,线成面。废话不多说,看看程序。
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace SmartDeviceProject1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- List<Point> listPoint = null;
- List<List<Point>> lineList = new List<List<Point>>();
- /// <summary>
- /// 画笔按下
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
- {
- listPoint = new List<Point>(); //创建点的集合
- }
- /// <summary>
- /// 画笔移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
- {
- this.AddPoint(e.X, e.Y);
- }
- /// <summary>
- /// 添加点
- /// </summary>
- /// <param name="pointX"></param>
- /// <param name="pointY"></param>
- private void AddPoint(int pointX,int pointY)
- {
- listPoint.Add(new Point(pointX, pointY));
- int pointCount = listPoint.Count;
- if (pointCount > 1)
- {
- Graphics gp = pictureBox1.CreateGraphics();
- Pen pen = new Pen(Color.Red);
- gp.DrawLine(pen, listPoint[pointCount - 2].X, listPoint[pointCount - 2].Y,listPoint[pointCount-1].X,listPoint[pointCount-1].Y);
- gp.Dispose();
- pen.Dispose();
- }
- }
- /// <summary>
- /// 画笔抬起
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
- {
- this.AddPoint(e.X,e.Y);
- lineList.Add(listPoint);//将所有的线条添加进来
- }
- /// <summary>
- /// 绘制线条
- /// </summary>
- private void DrawLines(Graphics gp)
- {
- Pen pen = new Pen(Color.Red);
- int lineCount = lineList.Count;
- foreach (List<Point> point in lineList)
- {
- gp.DrawLine(pen,point[0].X,point[0].Y,point[1].X,point[1].Y);
- }
- pen.Dispose();
- }
- //保存图片
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- saveFileDialog1.Filter = "Jpg图片|*.jpg|Gif图片|*.gif|BMP图片|*.bmp|PNG图片|*.png|图片|*.jpg;*.gif;*.bmp;*.png";
- saveFileDialog1.FilterIndex =1;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- if (lineList.Count > 0)
- {
- Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
- Graphics gp = Graphics.FromImage(bmp);
- Brush brush = new SolidBrush(Color.White);
- gp.FillRectangle(brush, pictureBox1.ClientRectangle);
- this.DrawLines(gp);
- bmp.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
- bmp.Dispose();
- gp.Dispose();
- brush.Dispose();
- }
- else
- {
- MessageBox.Show("你还没有绘图,请绘图", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
- }
- }
- }
- catch(Exception m)
- {
- MessageBox.Show("保存图片失败,原因:"+m.Message);
- }
- finally
- {
- lineList.Clear(); //保存完毕,清除所有线条
- }
- }
- /// <summary>
- /// 清除绘制的图形
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- lineList.Clear();
- pictureBox1.Invalidate();
- }
- }
- }
看看效果
点击保存,弹出保存框,如下
点击保存,完成图片的保存,然后我们去资源管理器看看有没有
保存成功,就是这么简单。
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/384688,如需转载请自行联系原作者