zoukankan      html  css  js  c++  java
  • 拼图设计 课程作业三

    这里有些函数我也看不懂耶,莫名奇妙的,也想过自己实现,不过觉得bug太多,不知道怎么改,就索性用书上代码 这个实验纯抄书吧

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 拼图设计
    {
        class CutPic 
        {
    
            //public static List<Bitmap> BitMapList1 { get => BitMapList; set => BitMapList = value; }
            public static string PicturePath = "";
            public static List<Bitmap> BitMapList = null;
    
            //保存图片到根目录的Picture文件下
            //path 文件路径 iWidth 宽 iHeight 高
            public static Image Reseize(string path, int iWidth, int iHeight)
            {
                Image thumbnail = null;
                try
                {
                    var img = Image.FromFile(path);
                    thumbnail = img.GetThumbnailImage(iWidth, iHeight, null, IntPtr.Zero);
                    thumbnail.Save(Application.StartupPath.ToString()+"\Picture\img.jpeg");
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
                return thumbnail;
            }
    
            //剪切图片
            // b 图片 X X坐标 Y Y坐标 iWidth 宽 iHeight 高
            public static Bitmap Cut(Image b,int X,int Y,int iWidth,int iHeight)
            {
                if (b == null)
                    return null;
                int w = b.Width, h = b.Height;
                if(X >= w || Y >= h)
                {
                    return null;
                }
                if(X + iWidth > w)//不够的话 就缩小边框的大小
                {
                    iWidth = w - X;
                }
                if(Y + iHeight > h)//不够的话 就缩小边框的大小
                {
                    iHeight = h - Y;
                }
                try
                {
                    Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                    Graphics g = Graphics.FromImage(bmpOut);
                    g.DrawImage( b,new Rectangle(0,0,iWidth,iHeight) ,new Rectangle(X,Y,iWidth,iHeight),GraphicsUnit.Pixel);//生成一个矩形图像
                    return bmpOut;   
                }
                catch
                {
                    return null;
                }
            }
            
        }
    }
    CutPic
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 拼图设计
    {
        public partial class Form_Main : Form
        {
            public Form_Main()
            {
                InitializeComponent();
                InitGame();
            }
            PictureBox[] pictureList = null; //图片位置数组
            SortedDictionary<string, Bitmap> pictureLocationDict = new SortedDictionary<string, Bitmap>();
            Point[] pointList = null;
            SortedDictionary<string, PictureBox> pictureBoxLocationDict = new SortedDictionary<string, PictureBox>();
            int second = 0;
            PictureBox currentPictureBox = null;
            PictureBox haveToPictureBox = null;
            Point oldLocation = Point.Empty;
            Point newLocation = Point.Empty;
            Point mouseDownPoint = Point.Empty;
            Rectangle rect = Rectangle.Empty;
            bool isDrag = false;
            public string originalpicpath;
    
            private int ImgNumbers
            {
                get
                {
                    return (int) this.numericUpDown1.Value;
                }
            }
            private int SideLength
            {
                get
                {
                    if (this.ImgNumbers != 0)
                        return 600 / this.ImgNumbers;
                    this.numericUpDown1.Value = 3;
                    return 200;
                }
            }
    
            //生成图片框矩阵
            private void InitRandomPictureBox()
            {
                if(ImgNumbers == 0 )
                {
                    MessageBox.Show("所选切割边数不能为0");
                    return;
                }
    
                pnl_Picture.Controls.Clear();
                pictureList = new PictureBox[ImgNumbers * ImgNumbers];
                pointList = new Point[ImgNumbers * ImgNumbers];
                for(int i=0; i<this.ImgNumbers; i++)
                {
                    for(int j=0; j<this.ImgNumbers; j++)
                    {
                        PictureBox pic = new PictureBox();//实例化 并且对pic做调整
                        pic.Name = "pictureBox" + (j + i * ImgNumbers + 1).ToString();
                        pic.Location = new Point(j * SideLength, i * SideLength);
                        pic.Size = new Size(SideLength, SideLength);
                        pic.Visible = true;
                        pic.BorderStyle = BorderStyle.FixedSingle;
                        pic.MouseDown += new MouseEventHandler(pictureBox_MouseDown);//增加鼠标点击效果
                        pic.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
                        pic.MouseUp += new MouseEventHandler(pictureBox_MouseUp);
                        pnl_Picture.Controls.Add(pic);//添加到pic_box上
                        pictureList[j + i * ImgNumbers] = pic;
                        pointList[j + i * ImgNumbers] = new Point(j * SideLength, i * SideLength);
                    }
                }
            }
    
            //切割图片
            public void Flow(string path,bool disorder)
            {
                InitRandomPictureBox();
                Image bm = CutPic.Reseize(path, 600, 600);//重新调整大小
                CutPic.BitMapList = new List<Bitmap>();
                for(int y=0; y<600; y += SideLength)//每个bit位的调整
                {
                    for(int x=0; x<600; x += SideLength)
                    {
                        Bitmap temp = CutPic.Cut(bm, x, y, SideLength, SideLength);
                        CutPic.BitMapList.Add(temp);
                    }
                }
                ImportBitMap(disorder);
            }
    
            public void ImportBitMap(bool disorder)
            {
                try
                {
                    int i = 0;
                    foreach(PictureBox item in pictureList)
                    {
                        Bitmap temp = CutPic.BitMapList[i];
                        item.Image = temp;
                        i++;
                    }
                    if (disorder)
                    {
                        ResetPictureLocation();
                    }
                }
                catch(Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
    
            public Point[] DisOrderLocation()
            {
                Point[] tempArray = (Point[])pointList.Clone();
                for (int i = tempArray.Length - 1; i > 0; i--)
                {
                    Random rand = new Random();
                    int p = rand.Next(i);
                    Point temp = tempArray[p];
                    tempArray[p] = tempArray[i];
                    tempArray[i] = temp;
                }
                return tempArray;
            }
    
            public void ResetPictureLocation()
            {
                Point[] temp = DisOrderLocation();
                int i = 0;
                foreach (PictureBox item in pictureList)
                {
                    item.Location = temp[i];
                    i++;
                }
            }
    
            public void InitGame()
            {
                if(!Directory.Exists(Application.StartupPath.ToString()+"\Picture" ))
                {
                    Directory.CreateDirectory(Application.StartupPath.ToString() + "\Picture");
                   // Properties.Resources.默认.Save(Application.StartupPath.ToString() + "\Picture\1.jpg");
                    Properties.Resources._1.Save(Application.StartupPath.ToString() + "\Picture\1.jpg");
                    Properties.Resources._2.Save(Application.StartupPath.ToString() + "\Picture\2.jpg");
                    Properties.Resources._3.Save(Application.StartupPath.ToString() + "\Picture\3.jpg");
                    Properties.Resources._4.Save(Application.StartupPath.ToString() + "\Picture\4.jpg");
                    Properties.Resources._5.Save(Application.StartupPath.ToString() + "\Picture\5.jpg");
                    Properties.Resources._6.Save(Application.StartupPath.ToString() + "\Picture\6.jpg");
                    
                }
                Random r = new Random();
                int i = r.Next(6);
                originalpicpath = Application.StartupPath.ToString() + "\Picture\" + i.ToString()+".jpg";
                Flow(originalpicpath, true);
            }
    
            public PictureBox GetPictureBoxByLocation(int x,int y)//lmada表达式找位置
            {
                PictureBox pic = null;
                foreach(PictureBox item in pictureList)
                {
                    if( ( x > item.Location.X ) && (y > item.Location.Y)  && (item.Location.X + SideLength > x) && (item.Location.Y + SideLength > y))
                    {
                        pic = item;
                    }
                }
                return pic;
            }
    
            private void pictureBox_MouseDown(object sender,MouseEventArgs e)
            {
                oldLocation = new Point(e.X, e.Y);
                currentPictureBox = GetPictureBoxByHashCode(sender.GetHashCode().ToString());
                MoseDown(currentPictureBox, sender, e);
            }
    
            public PictureBox GetPictureBoxByHashCode(string hascode)
            {
                PictureBox pic = null;
                foreach (PictureBox item in pictureList)
                {
                    if (hascode == item.GetHashCode().ToString())
                    {
                        pic = item;
                    }
                }
                return pic;
            }
    
            public void MoseDown(PictureBox pic ,object sender,MouseEventArgs e)
            {
                if(e.Button == MouseButtons.Left)
                {
                    oldLocation = e.Location;
                    rect = pic.Bounds;
                }
            }
    
            private void pictureBox_MouseMove(object sender,MouseEventArgs e)
            {
                if(e.Button == MouseButtons.Left)
                {
                    isDrag = true;
                    rect.Location = getPointToForm(new Point(e.Location.X - oldLocation.X,e.Location.Y - oldLocation.Y));
                    this.Refresh();
                }
            }
    
            private Point getPointToForm(Point p)
            {
                return this.PointToClient(pnl_Picture.PointToScreen(p));
            }
    
            private void reset()
            {
                mouseDownPoint = Point.Empty;
                rect = Rectangle.Empty;
                isDrag = false;
            }
    
            private void pictureBox_MouseUp(object sender,MouseEventArgs e)
            {
                oldLocation = new Point(currentPictureBox.Location.X, currentPictureBox.Location.Y);
                if(oldLocation.X + e.X > 600 || oldLocation.Y + e.Y > 600 || oldLocation.X +e.X <0 || oldLocation.Y + e.Y < 0)
                {
                    return;
                }
                haveToPictureBox = GetPictureBoxByLocation(oldLocation.X + e.X, oldLocation.Y + e.Y);
                newLocation = new Point(haveToPictureBox.Location.X, haveToPictureBox.Location.Y);
                haveToPictureBox.Location = oldLocation;
                PictureMouseUp(currentPictureBox,sender,e);
                if(second > 30)
                {
                    MessageBox.Show("用时超时");
                    timer1.Stop();
                    second = 0;
                    InitGame();
                    //time.Text = 0+ "    s";
                }
                if(Judge() && second <= 30)
                {
                    //lab_result.Text = "成功";
                    MessageBox.Show("恭喜拼图成功");
                    timer1.Stop();
                    second = 0;
                    InitGame();
                    //time.Text = 0 + "    s";
                }
            }
            
            public void PictureMouseUp(PictureBox pic ,object sender,MouseEventArgs e)
            {
                if(e.Button == MouseButtons.Left)
                {
                    if (isDrag)
                    {
                        isDrag = false;
                        pic.Location = newLocation;
                        this.Refresh();
                    }
                }
                reset();
            }
    
            public bool Judge()
            {
                bool result = true;
                int i = 0;
                foreach(PictureBox item in pictureList)
                {
                    if(item.Location != pointList[i])
                    {
                        result = false;
                    }
                    i++;
                }
                return result;
            }
    
            /*private void btn_import_Click(object sender, EventArgs e)
            {
                if(pb_pic.ShowDialog() == DialogResult.OK)
                {
                    //lab_result.Text = "";
                    originalpicpath = pb_pic.FileName;
                    CutPic.PicturePath = pb_pic.FileName;
                    Flow(CutPic.PicturePath, true);
                }
            }*/
    
            private void btn_Changepic_Click(object sender, EventArgs e)
            {
                Random r = new Random();
                int i = r.Next(7);
                originalpicpath = Application.StartupPath.ToString() + "\Picture\" + i.ToString() + ".jpg";
                Flow(originalpicpath, true);
            }
    
            private void btn_Reset_Click(object sender, EventArgs e)
            {
                Flow(originalpicpath, true);
            }
    
            private void btn_pic_Click(object sender, EventArgs e)
            {
                Form_pic ori = new Form_pic();
                ori.picpath = originalpicpath;
                ori.ShowDialog();
            }
    
            private void Form_Main_Load(object sender, EventArgs e)
            {
    
            }
    
            private void btn_import_Click(object sender, EventArgs e)
            {
                OpenFileDialog newPic = new OpenFileDialog();
                if (newPic.ShowDialog() == DialogResult.OK)
                {
                    originalpicpath = newPic.FileName;
                    //CutPic.PicturePath = newPic.FileName;
                    Flow(originalpicpath, true);
                }
            }
    
            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                InitGame();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                second++;
                time.Text = second + "    s";
            }
    
            private void btn_tiaozhan_Click(object sender, EventArgs e)
            {
    
                if (btn_tiaozhan.Text == "挑战模式")
                {
                    InitGame();
                    timer1.Start();
                    //btn_tiaozhan.Text = "暂停";
                }
                
            }
        }
    }
    Form1
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 拼图设计
    {
        public partial class Form_pic : Form
        {
            public string picpath
            {
                set;get;
            }
    
            public Form_pic()
            {
                InitializeComponent();
            }
    
            private void Form_pic_Load(object sender, EventArgs e)
            {
                pb_pic.Image = CutPic.Reseize(picpath, 600, 600);
            }
    
            private void pb_pic_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    Form2

    参考资料: 深入C#中get与set的详解 

  • 相关阅读:
    代理模式
    组合模式
    策略模式
    状态模式
    js 未结束的字符串常量错误解决方法
    struts2+hibernate+poi导出Excel实例
    Java 实现导出excel表 POI
    ExtJS 4.2 中自定义事件
    dhtmlxGrid分页查询,条件查询实例
    '@P0' 附近有语法错误
  • 原文地址:https://www.cnblogs.com/Draymonder/p/8257998.html
Copyright © 2011-2022 走看看