注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

告别迷茫

梦想与现实的差距,就是我们生活的意义。因为我们有差距,我们才会一直积累,在努力。

 
 
 

日志

 
 

C# 仿真Window 7 画图程序,基本功能  

2015-06-01 23:14:31|  分类: C#基础 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
C 仿真Window 7 画图程序,基本功能 - 983433479 - 告别迷茫
我的那个太复杂了,没有弄的好,就借鉴了同学的,弄的还是不错的!
代码的连接:http://pan.baidu.com/s/1kTKhzN5
1.填充 这个比较的复杂
详解见代码

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;
using System.IO;
namespace huatu
{
public partial class Form1 : Form
{
Point Sp;
private Image fimg;
private string tools;
private bool start = false;
Color c = Color.Black;
Stack<Bitmap> last = new Stack<Bitmap>();
Stack<Bitmap> first = new Stack<Bitmap>();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(new SolidBrush(pictureBox1.BackColor), new Rectangle(0, 0,pictureBox1.Width, pictureBox1.Height));
g.Dispose();
pictureBox1.Image = (Image)bmp.Clone();
last.Push(bmp);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Cross;
if (start == true && e.Button == MouseButtons.Left)
{
Bitmap img = (Bitmap)pictureBox1.Image.Clone();
Graphics Ng = Graphics.FromImage(img);
switch (tools)
{
case "pen":
{
Pen pen = new Pen(c, 1);
Point Cp = new Point(e.X, e.Y);
Ng.DrawLine(pen, Sp, Cp);
Sp = Cp;
break;
}
case "line":
{
Bitmap img1 = (Bitmap)fimg.Clone();
Graphics Ng1 = Graphics.FromImage(img1);
Pen pen = new Pen(c, 1);
Point Cp = new Point(e.X, e.Y);
Ng1.DrawLine(pen, Sp, Cp);
Ng1.Dispose();
img = img1;
break;
}
case "rectangle":
{
Bitmap img1 = (Bitmap)fimg.Clone();
Graphics Ng1 = Graphics.FromImage(img1);
Pen pen = new Pen(c, 1);
float width = Math.Abs(e.X - Sp.X);
float heigth = Math.Abs(e.Y - Sp.Y);
Point rSp = Sp;
if (e.X < Sp.X)
{
rSp.X = e.X;
}
if (e.Y < Sp.Y)
{
rSp.Y = e.Y;
}
Ng1.DrawRectangle(pen, rSp.X, rSp.Y, width, heigth);
img = img1;
break;
}
case "circle":
{
Bitmap img1 = (Bitmap)fimg.Clone();
Graphics Ng1 = Graphics.FromImage(img1);
Pen pen = new Pen(c, 1);
Ng1.DrawEllipse(pen, Sp.X, Sp.Y, e.X - Sp.X,
e.Y - Sp.Y);
Ng1.Dispose();
img = img1;
break;
}
case "gun":
{
Point b = new Point();
b.X = e.X;
b.Y = e.Y;
drawsprayGun(img, b);
break;
}

}
Ng.Dispose();
pictureBox1.Image = (Image)img.Clone();
}
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Bitmap img = (Bitmap)pictureBox1.Image.Clone();
Graphics Ng = Graphics.FromImage(img);
Sp = new Point(e.X, e.Y);
if (start == true && e.Button == MouseButtons.Left)
{
switch (tools)
{
case "pen":
{
Pen pen = new Pen(c, 1);
Point Cp = new Point(e.X, e.Y);
Ng.DrawLine(pen, Sp, Cp);
break;
}
case "line":
{
Pen pen = new Pen(c, 1);
Point Cp = new Point(e.X, e.Y);
break;
}
case "fill":
{
Point a = new Point();
a.X = e.X;
a.Y = e.Y;
drawFill(img,a);
break;
}
case "gun":
{
timer1.Start();
Point a = new Point();
a.X=e.X;
a.Y=e.Y;
drawsprayGun(img, a);
break;
}

}
Ng.Dispose();
fimg = (Image)img.Clone();
pictureBox1.Image = fimg;
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Bitmap img = (Bitmap)pictureBox1.Image.Clone();
Graphics Ng = Graphics.FromImage(img);
if (start == true)
{
switch (tools)
{
case "pen":
{
Pen pen = new Pen(c, 1);
Point Cp= new Point(e.X, e.Y);
Ng.DrawLine(pen, Sp, Cp);
Ng.Dispose();
break;
}
case "line":
{
Pen pen = new Pen(c, 1);
Point Cp= new Point(e.X, e.Y);
Graphics plantpic = Ng;
plantpic.DrawLine(pen, Sp, Cp);
break;
}
case "Circle":
{
Pen pen = new Pen(c, 1);
Ng.DrawEllipse(pen, Sp.X, Sp.Y, e.X - Sp.X,
e.Y - Sp.Y);
break;
}
case "rectangle":
{
Pen pen = new Pen(c, 1);
float width = Math.Abs(e.X - Sp.X);
float heigth = Math.Abs(e.Y - Sp.Y);
Point rSp =Sp;
if (e.X < Sp.X)
{
rSp.X = e.X;
}
if (e.Y < Sp.Y)
{
rSp.Y = e.Y;
}
Ng.DrawRectangle(pen, rSp.X, rSp.Y, width, heigth);
break;
}
case "gun":
{

timer1.Stop();
break;
}
}
last.Push(img);
Ng.Dispose();
pictureBox1.Image = (Image)img.Clone();
}
}



private void drawFill(Bitmap img,Point e)
{
Stack<Point> pointStack = new Stack<Point>(10000);
pointStack.Push(e);
int[,] steps = new int[4, 2] { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 } };
while (pointStack.Count > 0)
{
Point p1 = pointStack.Pop();
img.SetPixel(p1.X, p1.Y, c);
Point np;
for (int i = 0; i < 4; i++)
{
np = new Point(p1.X + steps[i, 0], p1.Y + steps[i, 1]);
if (np.X >= 0 && np.X < img.Width && np.Y >= 0 && np.Y < img.Height)
if (img.GetPixel(np.X, np.Y).ToArgb() != Color.Black.ToArgb())
pointStack.Push(np);
}
img.SetPixel(p1.X, p1.Y, c);
}
;
}

private void drawsprayGun(Bitmap img,Point e)
{
const int r = 10;
int base_x, base_y, x, y, n;
Random rnd = new Random();
base_x = e.X;
base_y = e.Y;
n = 0;
while (n < 100)
{
x = rnd.Next(-r, r);
y = rnd.Next(-r, r);
if (x * x + y * y < r * r &&e.X>0&&e.X<pictureBox1.Width-10&&e.Y>0&&e.Y<pictureBox1.Height)
{
img.SetPixel(base_x + x, base_y + y ,c);
n++;
}
}

}


private void circlecToolStripMenuItem_Click(object sender, EventArgs e)
{
linelToolStripMenuItem.Checked = false;
penpToolStripMenuItem.Checked = false;
fillfToolStripMenuItem.Checked = false;
sprayGunsToolStripMenuItem.Checked = false;
circlecToolStripMenuItem.Checked = true;
rectanglerToolStripMenuItem.Checked = false;
tools = "circle";
start = true;
}

private void rectanglerToolStripMenuItem_Click(object sender, EventArgs e)
{
linelToolStripMenuItem.Checked = false;
penpToolStripMenuItem.Checked = false;
fillfToolStripMenuItem.Checked = false;
sprayGunsToolStripMenuItem.Checked = false;
circlecToolStripMenuItem.Checked = false;
rectanglerToolStripMenuItem.Checked = true;
tools = "rectangle";
start = true;
}

private void 新建nToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap nbitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(nbitmap);

pictureBox1.Image = nbitmap;
g.Dispose();
}

private void 保存是ToolStripMenuItem_Click(object sender, EventArgs e)
{
bool isSave = true;
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "图片保存";
saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";

if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveImageDialog.FileName.ToString();

if (fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();

System.Drawing.Imaging.ImageFormat imgformat = null;

if (fileExtName != "")
{
switch (fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case "gif":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
default:
MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
isSave = false;
break;
}

}

//默认保存为JPG格式
if (imgformat == null)
{
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
}

if (isSave)
{
try
{
this.pictureBox1.Image.Save(fileName, imgformat);
//MessageBox.Show("图片已经成功保存!");
}
catch
{
MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
}
}

}

}
}

private void 前一步ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (first.Count > 0)
{
last.Push(first.Peek());
pictureBox1.Image = first.Peek();
first.Pop();
}
else
MessageBox.Show("Not exist!");
}

private void 后一步ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (last.Count > 1)
{
first.Push(last.Peek());
last.Pop();
pictureBox1.Image = last.Peek();
}
else
pictureBox1.Image = last.Peek();
}

private void linelToolStripMenuItem_Click(object sender, EventArgs e)
{
linelToolStripMenuItem.Checked = true;
penpToolStripMenuItem.Checked = false;
fillfToolStripMenuItem.Checked = false;
sprayGunsToolStripMenuItem.Checked = false;
circlecToolStripMenuItem.Checked = false;
rectanglerToolStripMenuItem.Checked = false;
tools = "line";
start = true;
}

private void penpToolStripMenuItem_Click(object sender, EventArgs e)
{
linelToolStripMenuItem.Checked = false;
penpToolStripMenuItem.Checked = true;
fillfToolStripMenuItem.Checked = false;
sprayGunsToolStripMenuItem.Checked = false;
circlecToolStripMenuItem.Checked = false;
rectanglerToolStripMenuItem.Checked = false;
tools = "pen";
start = true;
}

private void fillfToolStripMenuItem_Click(object sender, EventArgs e)
{
linelToolStripMenuItem.Checked = false;
penpToolStripMenuItem.Checked = false;
fillfToolStripMenuItem.Checked = true;
sprayGunsToolStripMenuItem.Checked = false;
circlecToolStripMenuItem.Checked = false;
rectanglerToolStripMenuItem.Checked = false;
tools = "fill";
start = true;
}

private void sprayGunsToolStripMenuItem_Click(object sender, EventArgs e)
{
linelToolStripMenuItem.Checked = false;
penpToolStripMenuItem.Checked = false;
fillfToolStripMenuItem.Checked = false;
sprayGunsToolStripMenuItem.Checked = true;
circlecToolStripMenuItem.Checked = false;
rectanglerToolStripMenuItem.Checked = false;
tools = "gun";
start = true;
}

private void button1_Click(object sender, EventArgs e)
{
c = Color.Black;
}

private void red_Click(object sender, EventArgs e)
{
c = Color.Red;
}

private void button3_Click(object sender, EventArgs e)
{
c = Color.Green;
}

private void button4_Click(object sender, EventArgs e)
{
c = Color.Blue;
}

private void 退出eToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void 打开oToolStripMenuItem_Click(object sender, EventArgs e)
{
string imgPath = null;
string directory = null;
List<string> imgArray = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
openFileDialog1.Filter = "(*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.wmf;*.tiff)|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.wmf;*.tiff";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
imgPath = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(imgPath);
directory = Path.GetDirectoryName(imgPath);
string[] imgarray = Directory.GetFiles(directory);
var result = from imgstring in imgarray
where imgstring.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
imgstring.EndsWith("jpeg", StringComparison.OrdinalIgnoreCase) ||
imgstring.EndsWith("png", StringComparison.OrdinalIgnoreCase) ||
imgstring.EndsWith("bmp", StringComparison.OrdinalIgnoreCase) ||
imgstring.EndsWith("gif", StringComparison.OrdinalIgnoreCase) ||
imgstring.EndsWith("wmf", StringComparison.OrdinalIgnoreCase) ||
imgstring.EndsWith("tiff", StringComparison.OrdinalIgnoreCase)
select imgstring;


imgArray = result.ToList();
}
}

private void timer1_Tick(object sender, EventArgs e)
{
Point p1 = MousePosition;
Point a = this.PointToClient(p1);
a.X = a.X - pictureBox1.Left;
a.Y = a.Y - pictureBox1.Top;
Bitmap img = (Bitmap)pictureBox1.Image.Clone();
drawsprayGun(img, a);
pictureBox1.Image = img;
}


}
}



 
  评论这张
 
阅读(13)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017