基本信息
源码名称:C# 拼图 例子源码(可任意拖动、旋转、删除图片)
源码大小:0.18M
文件格式:.rar
开发语言:C#
更新时间:2016-03-22
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

就是拼图,亲测通过,左右键旋转图片,delete键删除图片,拖动图片

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.Drawing.Imaging;

namespace ImageCombineCSharp
{
    public partial class Form1 : Form
    {
        Point pS;
        public Form1()
        {
            InitializeComponent();

        }

        private void 读入图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //属性
                PictureBox pb = new PictureBox();
                pb.Location = new Point(panel1.Left, panel1.Top);
                pb.Size = new System.Drawing.Size(100, 100);
                pb.SizeMode = PictureBoxSizeMode.AutoSize;
                pb.BackColor = Color.Transparent;
                pb.ImageLocation = openFileDialog1.FileName;

                //方法
                pb.MouseDown  = new System.Windows.Forms.MouseEventHandler(this.MaskPicture_MouseDown);
                pb.MouseUp  = new System.Windows.Forms.MouseEventHandler(this.MaskPicture_MouseUp);
                pb.Click  = new System.EventHandler(MaskPicture_Click);
                pb.KeyDown  = new KeyEventHandler(MaskPicture_KeyDown);

                //添加
                panel1.Controls.Add(pb);

                pb.Focus();
                pb.BringToFront();
            }
        }

        private void MaskPicture_MouseDown(object sender, MouseEventArgs e)
        {
            pS.X = e.X;
            pS.Y = e.Y;
        }
        private void MaskPicture_MouseUp(object sender, MouseEventArgs e)
        {
            ((PictureBox)sender).Left  = e.X - pS.X;
            ((PictureBox)sender).Top  = e.Y - pS.Y;
        }
        private void MaskPicture_Click(object sender, EventArgs e)
        {
             foreach (Control c in this.panel1.Controls)
             {
                 if (c is PictureBox)
                 {
                     if (((PictureBox)c).BorderStyle == BorderStyle.Fixed3D)
                     {
                         ((PictureBox)c).BorderStyle = BorderStyle.FixedSingle;
                     }
                 }
             }
            
            ((PictureBox)sender).Focus();
            ((PictureBox)sender).BringToFront();
            ((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;

        }
        private void MaskPicture_KeyDown(object sender, KeyEventArgs e)
        {
            Bitmap bmp = null;
            switch (e.KeyValue)
            {
                case 46://delete
                    if (DialogResult.OK == MessageBox.Show("是否从项目中移除该图片?", "移除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                    {
                        panel1.Controls.Remove((PictureBox)sender);
                    }
                    break;
                case 37://←
                    bmp = ImageTools.KiRotate(((PictureBox)sender).Image as Bitmap, -1.0f, Color.Transparent);
                    ((PictureBox)sender).Image = null;
                    ((PictureBox)sender).Image = bmp;
                    
                    break;
                case 39://→
                    bmp = ImageTools.KiRotate(((PictureBox)sender).Image as Bitmap, 1.0f, Color.Transparent);
                    ((PictureBox)sender).Image = null;
                    ((PictureBox)sender).Image = bmp;
                    break;
            }
        }

        private void 保存图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "JPG图片|*.jpg";
            saveFileDialog1.Title = "选择你保存的位置";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;

            Point lastPictureLeftTop = new Point();
            Point lastPictureRightBottom = new Point();

            lastPictureLeftTop.X = panel1.Left   panel1.Width;
            lastPictureLeftTop.Y = panel1.Top   panel1.Height;
            lastPictureRightBottom.X = panel1.Left;
            lastPictureRightBottom.Y = panel1.Top;

            foreach (Control c in this.panel1.Controls)
            {
                if (c is PictureBox)
                {
                    if (lastPictureLeftTop.X > c.Left) lastPictureLeftTop.X = c.Left;
                    if (lastPictureLeftTop.Y > c.Top) lastPictureLeftTop.Y = c.Top;

                    if (lastPictureRightBottom.X < c.Left   c.Width) lastPictureRightBottom.X = c.Left   c.Width;
                    if (lastPictureRightBottom.Y < c.Top   c.Height) lastPictureRightBottom.Y = c.Top   c.Height;
                }
            }

            int lastPictureWidth = lastPictureRightBottom.X - lastPictureLeftTop.X;
            int lastPictureHeight = lastPictureRightBottom.Y - lastPictureLeftTop.Y;
            Bitmap bmp = new Bitmap(lastPictureWidth, lastPictureHeight);

            foreach (Control c in this.panel1.Controls)
            {
                if (c is PictureBox)
                {
                   for (int i = 0;i < (c as PictureBox).Image.Height;i  )
                  {
                      for (int j = 0; j < (c as PictureBox).Image.Width; j  )
                      {
                          if (((c as PictureBox).Image as Bitmap).GetPixel(j, i) != Color.Transparent)
                          {
                               bmp.SetPixel(j   c.Left - lastPictureLeftTop.X, i   c.Top - lastPictureLeftTop.Y, ((c as PictureBox).Image as Bitmap).GetPixel(j, i));
                              
                          }
                      }
                   }
                    
//                     BitmapData coverBmpData = ((c as PictureBox).Image as Bitmap).LockBits(new Rectangle(0, 0,
//                         ((c as PictureBox).Image as Bitmap).Width, ((c as PictureBox).Image as Bitmap).Height),
//                         ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
//                     int coverWidth = ((c as PictureBox).Image as Bitmap).Width;
//                     int coverHeight = ((c as PictureBox).Image as Bitmap).Height;
//                     int coverOffset = coverBmpData.Stride - coverBmpData.Width * 4;
// 
//                     try
//                     {
//                         unsafe
//                         {
//                             byte* pCoverImage = (byte*)(coverBmpData.Scan0.ToPointer());
//                             for (int y = 0; y < Height; y  )
//                             {
//                                 for (int x = 0; x < Width; x  )
//                                 {
//                                     if (((c as PictureBox).Image as Bitmap).GetPixel(x, y) != Color.Transparent)
//                                     {
//                                         bmp.SetPixel(x   c.Left - lastPictureLeftTop.X, y   c.Top - lastPictureLeftTop.Y, Color.FromArgb(pCoverImage[0], pCoverImage[3], pCoverImage[2], pCoverImage[1]));
//                                     }
//                                     pCoverImage  = 4;
//                                 }
//                                 pCoverImage  = coverOffset;
//                             }
//                             ((c as PictureBox).Image as Bitmap).UnlockBits(coverBmpData);
//                         }
//                     }
//                     catch (System.Exception ex)
//                     {
//                     	
//                     }
                }
            }

            bmp.Save(saveFileDialog1.FileName);

            MessageBox.Show("保存成功", "保存成功");
        }
    }
}