如何找到开始和结束像素裁剪图像?

时间:2016-04-05 09:54:58

标签: c# image

我是一名初学程序员,我将图像分成16个部分(我正在制作图片益智游戏)并且每个人都在一个图片框内随机放置它们 改变现在我想知道每个图像的开始和结束我切割它连接到另一个图像(我的意思是,任何图像继续其他)。这样的事情可能吗?你有更好的主意吗?

namespace ImagePuzzelGame
{
    public partial class Form1 : Form
    {
        Point move;
        bool isDragging = false;
        public Dictionary<int, Point> pics
        {
            set;
            get;
        }
        private Point _defaultlocation = new Point(306, 306);
        Random rnd = new Random();
        public Point DefaultLocation
        {
            get
            {
                return _defaultlocation;
            }

            set
            {
                _defaultlocation = value;
            }
        }
        public List<PictureBox> picPuzzle
        {
            get;
            set;
        }
        public Bitmap newImage
        {
            get; set;
        }
        public Form1()
        {
            InitializeComponent();

            pics = new Dictionary<int, Point>()
            {
                {1,new Point(0,0) },
                {2,new Point(100,0) },
                {3,new Point(200,0) },
                {4,new Point(300,0) },
                {5,new Point(400,0) },
                {6,new Point(0,119) },
                {7,new Point(100,119) },
                {8,new Point(200,119) },
                {9,new Point(300,119) },
                {10,new Point(400,119) },
                {11,new Point(0,325) },
                {12,new Point(100,325) },
                {13,new Point(200,325) },
                {14,new Point(300,325) },
                {15,new Point(400,325) },
            };
            List<PictureBox> picturebox = new List<PictureBox>
            {
                pictureBox1,pictureBox2,pictureBox3,pictureBox4,pictureBox5,pictureBox6,pictureBox7,
                pictureBox8,pictureBox9,pictureBox10,pictureBox11,pictureBox12,pictureBox13,pictureBox14,pictureBox15,pictureBox16
            };

            picPuzzle = picturebox;

          resizeImage();
           cropImage();
           shufflePuzzle();
        }

        public void shufflePuzzle()
        {
            int numberSelect;
            int index = 0;
            Point virtualLocation;
            List<int> selected = new List<int>();
            while (selected.Count < picPuzzle.Count)
            {
                int randomSelectPuzzle = rnd.Next(0, 16);
                if (selected.Contains(randomSelectPuzzle))
                {
                    randomSelectPuzzle = rnd.Next(0, 16);
                }
                else
                {
                    numberSelect = randomSelectPuzzle;
                    virtualLocation = new Point(picPuzzle[numberSelect].Location.X, picPuzzle[numberSelect].Location.Y);

                    picPuzzle[numberSelect].Location = new Point(DefaultLocation.X, DefaultLocation.Y);
                    DefaultLocation = virtualLocation;
                    selected.Add(randomSelectPuzzle);
                }
            }
        }

        public void resizeImage()
        {
            Bitmap newImage = new Bitmap(320, 320);
            Bitmap srcImage = new Bitmap(@"C:\Users\bbmm\Desktop\images\lukas-graham.jpg");
            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode = SmoothingMode.HighQuality;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(srcImage, new Rectangle(0, 0, 320, 320));
            }

            this.newImage = newImage;

            try
            {
                Bitmap bt = new Bitmap(newImage);
                bt.Save(@"E:\img\pic.jpg");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }    
        }

        public void cropImage()
        {
            var filename = @"E:\img\pic.jpg";

            Rectangle cropRect;
            Bitmap[] src = new Bitmap[16];
            Bitmap[] target = new Bitmap[16];
            int x = 0, y = 0;
            int index = 0;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (index == 17)
                    {
                        return;
                    }
                    cropRect = new Rectangle(new Point(x, y), new Size(new Point(100, 100)));
                    src[index] = Image.FromFile(filename) as Bitmap;
                    target[index] = new Bitmap(cropRect.Width, cropRect.Height);
                    using (Graphics g = Graphics.FromImage(target[index]))
                    {
                        g.DrawImage(src[index], new Rectangle(0, 0, target[index].Width, target[index].Height), cropRect, GraphicsUnit.Pixel);
                    }
                    picPuzzle[index].Image = target[index];

                    x += 100;
                    index++;

                }
                y += 100;
                x = 0;
            }

        }

        public Point HoldLocation
        {
            get;
            set;
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            isDragging = true;
            move = e.Location;
        }

        public List<PictureBox> pastePics { get; set; }

        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {


            if (isDragging)
            {
                PictureBox pb = (PictureBox)sender;

                pb.Left += e.X - move.X;
                pb.Top += e.Y - move.Y;
                pb.BringToFront();
            }  

        }  

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
        }
    }
}

0 个答案:

没有答案