C#实现拼图效果

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;
using System.Collections;

using System.Diagnostics;
using System.IO;

namespace CopyPicture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<Image> imageList = new List<Image>();
            try
            {

                var path= Path.GetFullPath("..\\img.jpeg");
                Image img = Image.FromFile(path);
                imageList.Add(img);
                imageList.Add(img);
                imageList.Add(img);
          
                var imageobj1 = JoinImage(imageList, JoinMode.Horizontal);
                imageobj1.Save("..\\Newimg1.jpeg");
              var  imageobj2 = JoinImage(imageList, JoinMode.Vertical);
                imageobj2.Save("..\\Newimg2.jpeg");
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }


        enum JoinMode
        {
            Horizontal=0,
            Vertical=1
        }
        private Image JoinImage(List<Image> imageList, JoinMode jm)
        {
            //图片列表
            if (imageList.Count <= 0)
                return null;
            if (jm == JoinMode.Horizontal)
            {
                //横向拼接
                int width = 0;
                //计算总长度
                foreach (Image i in imageList)
                {
                    width += i.Width;
                }
                //高度不变
                int height = imageList.Max(x => x.Height);
                //构造最终的图片白板
                Bitmap tableChartImage = new Bitmap(width, height);
                Graphics graph = Graphics.FromImage(tableChartImage);
                //初始化这个大图
                graph.DrawImage(tableChartImage, width, height);
                //初始化当前宽
                int currentWidth = 0;
                foreach (Image i in imageList)
                {
                    //拼图
                    graph.DrawImage(i, currentWidth, 0);
                    //拼接改图后,当前宽度
                    currentWidth += i.Width;
                }
                return tableChartImage;
            }
            else if (jm == JoinMode.Vertical)
            {
                //纵向拼接
                int height = 0;
                //计算总长度
                foreach (Image i in imageList)
                {
                    height += i.Height;
                }
                //宽度不变
                int width = imageList.Max(x => x.Width);
                //构造最终的图片白板
                Bitmap tableChartImage = new Bitmap(width, height);
                Graphics graph = Graphics.FromImage(tableChartImage);
                //初始化这个大图
                graph.DrawImage(tableChartImage, width, height);
                //初始化当前宽
                int currentHeight = 0;
                foreach (Image i in imageList)
                {
                    //拼图
                    graph.DrawImage(i, 0, currentHeight);
                    //拼接改图后,当前宽度
                    currentHeight += i.Height;
                }
                return tableChartImage;
            }
            else
            {
                return null;
            }
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值