以前大学时候练图像绘制、旋转、切割的小练习,分享给学习winform的使用。贡献给.Net生态
下载地址:
https://download.csdn.net/download/zhanglianzhu_91/11055691
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;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Media;
namespace Jigsaw
{
public partial class Main : Form
{
Grid grid;
int yNum = 2;
int xNum = 2;
int buShu = 0;
int time = 0;
Image image;
List<Grade> list=new List<Grade> ();
System.Media.SoundPlayer sundPlayer = new SoundPlayer();
bool play = true;
bool isEnd = false;
public Main()
{
InitializeComponent();
}
///
/// 初始化函数
///
///
///
private void Main_Load(object sender, EventArgs e)
{
image=Image.FromFile(Application.StartupPath+"\\1.jpg");
grid=new Grid (image,xNum,yNum);
Bitmap bit = null;
if (image.Width < image.Height)
{
bit = new Bitmap(image.Width, image.Width);
}
else
{
bit = new Bitmap(image.Height, image.Height);
}
Graphics g = Graphics.FromImage(bit);
g.DrawImage(image, 0, 0);
image = bit;
pictureBox1.Image = image;
pictureBox1.Image = image;
try
{
FileStream fs = new FileStream(Application.StartupPath + "\\grade", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
list = (List<Grade>)formatter.Deserialize(fs);
fs.Close();
ShowGrad();
}
catch (Exception)
{
}
}
///
/// Paint函数
///
///
///
private void Main_Paint(object sender, PaintEventArgs e)
{
if (grid != null)
{
grid.Draw(this.CreateGraphics());
}
}
///
/// 鼠标单击事件
///
///
///
private void Main_MouseClick(object sender, MouseEventArgs e)
{
if (e.X > 10 && e.X < 505&&e.Y>50&&e.Y<545)
{
BeginNew();
}
if (e.X < 10 || e.X > 505)
{
return;
}
if (e.Y < 50 || e.Y > 545)
{
return;
}
Rectangle rect = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum)+11, grid.Cells[0, 0].CurNum /yNum * (500 / xNum)+51, image.Width / xNum-2, 500 / yNum-2);
Rectangle rectUp = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51 - (500 / xNum), 500 / xNum - 2, 500 / yNum - 2);
Rectangle rectLow = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51 + (500 / xNum), 500 / xNum - 2, 500 / yNum - 2);
Rectangle rectLeft = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) - (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51, 500 / xNum - 2, 500 / yNum - 2);
Rectangle rectRight = new Rectangle(grid.Cells[0, 0].CurNum % yNum * (500 / yNum) + (500 / yNum) + 11, grid.Cells[0, 0].CurNum / yNum * (500 / xNum) + 51, 500 / xNum - 2, 500 / yNum - 2);
int x=e.X;
int y=e.Y;
if(IsInRect(x,y,rectUp))
{
int curNum = grid.Cells[0, 0].CurNum;
Cell cell = FindCell(curNum - yNum);
grid.Cells[0, 0].CurNum = cell.CurNum;
cell.CurNum = curNum;
buShu++;
}
if (IsInRect(x, y, rectLow))
{
int curNum = grid.Cells[0, 0].CurNum;
Cell cell = FindCell(curNum+yNum);
grid.Cells[0, 0].CurNum = cell.CurNum;
cell.CurNum = curNum;
buShu++;
}
if (IsInRect(x, y, rectLeft))
{
int curNum = grid.Cells[0, 0].CurNum;
Cell cell = FindCell(curNum -1);
grid.Cells[0, 0].CurNum = cell.CurNum;
cell.CurNum = curNum;
buShu++;
}
if (IsInRect(x, y, rectRight))
{
int curNum = grid.Cells[0, 0].CurNum;
Cell cell = FindCell(curNum+1);
grid.Cells[0, 0].CurNum = cell.CurNum;
cell.CurNum = curNum;
buShu++;
}
grid.Draw(this.CreateGraphics());
label3.Text = "步数:" + buShu;
if (IsEnd())
{
timer1.Enabled = false;
grid.IsEnd = true;
grid.Draw(this.CreateGraphics());
isEnd = true;
MessageBox.Show("完成", "完成提示");
PaiMing paiMing = new PaiMing();
int count = 0;
foreach (Grade g in list)
{
if(g.Level==Convert.ToInt32(comboBox1.Text))
{
count++;
}
}
if (count<= 5)
{
if (paiMing.ShowDialog() == DialogResult.OK)
{
if (paiMing.textBox1.Text == "")
{
return;
}
Grade gra = new Grade();
gra.Name = paiMing.textBox1.Text;
gra.Step = buShu;
gra.Time = time;
time = 0;
gra.Level = Convert.ToInt32(comboBox1.Text);
list.Add(gra);
}
}
else
{
foreach (Grade g in list)
{
if (g.Level != Convert.ToInt32(comboBox1.Text))
{
continue;
}
if (time < g.Time)
{
int i = 0;
l1:
Grade gr=list[i];
if (gr.Level != Convert.ToInt32(comboBox1.Text))
{
if (i < list.Count-1)
{
i++;
}
goto l1;
}
foreach (Grade g1 in list)
{
if (g1.Level != Convert.ToInt32(comboBox1.Text))
{
continue;
}
if (g1.Time > gr.Time)
{
gr = g1;
}
}
if (paiMing.ShowDialog() == DialogResult.OK)
{
if (paiMing.textBox1.Text == "")
{
return;
}
Grade gra = new Grade();
gra.Name = paiMing.textBox1.Text;
gra.Step = buShu;
gra.Time = time;
gra.Level = Convert.ToInt32(comboBox1.Text);
time = 0;
list.Add(gra);
list.Remove(gr);
}
break;
}
}
}
File.Delete(Application.StartupPath + "\\grade");
FileStream stream = new FileStream(Application.StartupPath+"\\grade", FileMode.CreateNew);
BinaryFormatter formater = new BinaryFormatter();
formater.Serialize(stream,list);
stream.Close();
ShowGrad();
buShu = 0;
}
}
///
/// 开始新的一次
///
private void BeginNew()
{
if (isEnd)
{
buShu = 0;
time = 0;
timer1.Enabled = true;
xNum = Convert.ToInt32(comboBox1.Text) + 1;
yNum = xNum;
grid = new Grid(image, xNum, yNum);
grid.Draw(this.CreateGraphics());
ShowGrad();
isEnd = false;
return;
}
}
///
/// 判断点是否在矩形
///
///
///
///
///
private bool IsInRect(int x,int y,Rectangle rect)
{
if (x > rect.X && x < rect.X + rect.Width && y > rect.Y && y < rect.Y + rect.Height)
{
return true;
}
else
{
return false;
}
}
///
/// 找单元
///
///
///
private Cell FindCell(int num)
{
for (int i = 0; i < xNum; i++)
{
for (int j = 0; j < yNum; j++)
{
if (grid.Cells[i, j].CurNum == num)
{
return grid.Cells[i, j];
}
}
}
return null;
}
///
/// 判断结束
///
///
private bool IsEnd()
{
for (int i = 0; i < xNum; i++)
{
for (int j = 0; j < yNum; j++)
{
if (grid.Cells[i, j].Num != grid.Cells[i, j].CurNum)
{
return false;
}
}
}
return true;
}
///
/// 选择级别
///
///
///
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
buShu = 0;
time = 0;
timer1.Enabled = true;
xNum = Convert.ToInt32(comboBox1.Text)+1;
yNum = xNum;
grid = new Grid(image, xNum, yNum);
Bitmap bk = new Bitmap(600,600);
Graphics g = Graphics.FromImage(bk);
Brush brush=new SolidBrush(this.BackColor);
g.FillRectangle(brush, 0, 0, 600, 600);
this.CreateGraphics().DrawImage(bk, 0, 0);
grid.Draw(this.CreateGraphics());
ShowGrad();
}
///
/// 打开事件
///
///
///
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "JPG 图片|*.jpg|All Files|*.*";
op.InitialDirectory = Application.StartupPath;
op.ShowDialog();
if (op.FileName != "")
{
image = Image.FromFile(op.FileName);
Bitmap bit=null;
if (image.Width < image.Height)
{
bit = new Bitmap(image.Width, image.Width);
}
else
{
bit = new Bitmap(image.Height, image.Height);
}
Graphics g = Graphics.FromImage(bit);
g.DrawImage(image,0,0);
image = bit;
pictureBox1.Image = image;
xNum = Convert.ToInt32(comboBox1.Text) + 1;
yNum = xNum;
grid = new Grid(image, xNum, yNum);
grid.Draw(this.CreateGraphics());
buShu = 0;
time = 0;
timer1.Enabled = true;
}
}
///
/// 显示排名
///
private void ShowGrad()
{
listBox1.Items.Clear();
List<Grade> list1 = new List<Grade>();
List<Grade> list2 = new List<Grade>();
foreach (Grade g2 in list)
{
if (g2.Level == Convert.ToInt32(comboBox1.Text))
{
list2.Add(g2);
}
}
if (list2.Count == 0)
{
return;
}
l:
Grade gr = list2[0];
foreach (Grade g1 in list2)
{
if (g1.Time < gr.Time)
{
gr = g1;
}
}
list1.Add(gr);
list2.Remove(gr);
if (list2.Count != 0)
{
goto l;
}
if (list1.Count == 0)
{
return;
}
int i = 1;
foreach (Grade g1 in list1)
{
listBox1.Items.Add(g1.Name);
listBox1.Items.Add("第"+i+"名:"+"时间:" + g1.Time + "秒, 步数:" + g1.Step);
i++;
listBox1.Items.Add("---------------------------------");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (play)
{
sundPlayer.SoundLocation = Application.StartupPath + "\\voice.wav";
sundPlayer.Load();
sundPlayer.PlayLooping();
play = false;
this.button1.Text = "关闭";
}
else
{
sundPlayer.SoundLocation = Application.StartupPath + "\\voice.wav";
sundPlayer.Stop();
play = true;
this.button1.Text = "播放";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
time++;
label4.Text = "时间:" + time;
}
///
/// 帮助
///
///
///
private void btnHelpe_Click(object sender, EventArgs e)
{
Help help = new Help();
help.Show();
}
private void btnBout_Click(object sender, EventArgs e)
{
About about = new About();
about.Show();
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
}