首页 > 学院 > 开发设计 > 正文

winfrom-图片拆分成多块

2019-11-11 01:39:03
字体:
来源:转载
供稿:网友

先在panel容器(长宽为300)中绘制一个宫格,比如下面这样:

PRivate void Form1_Load(object sender, EventArgs e)        {            this.InitRandomPictureBox();        }                private void InitRandomPictureBox()        {            this.pnl_Random.Controls.Clear();            for (int i = 0; i < this.ImgNumbers; i++)            {                for (int j = 0; j < this.ImgNumbers; j++)                {                    PictureBox pic = new PictureBox();                    pic.Location = new Point(j * this.SideLength, i * this.SideLength);                    pic.Size = new Size(this.SideLength, this.SideLength);                    pic.Visible = true;                    pic.BorderStyle = BorderStyle.FixedSingle;                    this.pnl_Random.Controls.Add(pic);                }            }        }
/// <summary>        /// 每个方向上要切割成的块数        /// </summary>        private int ImgNumbers        {            get            {                return 3;            }        }        /// <summary>        /// 要切割成的正方形图片边长        /// </summary>        private int SideLength        {            get            {                return 300 / this.ImgNumbers; //300:panel容器大小            }        }//图片切割:
/// <summary>        /// 将图片切割成小图片,图片顺序为先水平后垂直        /// </summary>        /// <param name="cx">水平方向多少个图片</param>        ///<param name="cy">垂直方向多少个图片</param>        public static Image[] SplitToSmallImages(this Image fromImage, int cx, int cy)        {            Image[] imgs = new Image[cx * cy];            int nWidth = fromImage.Width / cx;            int nHeight = fromImage.Height / cy;            Bitmap image = new Bitmap(nWidth, nHeight);            Graphics graphics = Graphics.FromImage(image);            for (int i = 0; i < cy; i++)            {                for (int j = 0; j < cx; j++)                {                    graphics.DrawImage(fromImage, 0, 0, new Rectangle(j * nWidth, i * nHeight, nWidth, nHeight), GraphicsUnit.Pixel);                    Image img = Image.FromHbitmap(image.GetHbitmap());                    int idx = j + i * cx;                    img.Tag = idx;                    imgs[idx] = img;                }            }            return imgs;        }将切好的图片随机排序然后放入到容器中:
var tmp = this._splitImages.OrderBy(p => Guid.NewGuid()).ToList();            for (int i = 0; i < tmp.Count; i++)            {                this._splitPictureBoxes[i].Image = tmp[i];            }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表