图片和文字合并


        /// <summary>
    /// 图片合并帮助类
    /// </summary>
    public static class MergeImageHelper
    {

        //下载图片
        public static string DownloadImg(string strPath, string strName)
        {
            WebClient my = new WebClient();
            byte[] mybyte;
            mybyte = my.DownloadData(strPath);
            MemoryStream ms = new MemoryStream(mybyte);
            System.Drawing.Image img;
            img = System.Drawing.Image.FromStream(ms);
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "imgUploads\\ActivtyImg\\" + strName;
            img.Save(filePath, ImageFormat.Png);   //保存
            return filePath;
        }

        //调整图像大小
        public static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
        {
            //获取图片宽度
            int sourceWidth = imgToResize.Width;
            //获取图片高度
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
            //计算宽度的缩放比例
            nPercentW = ((float)size.Width / (float)sourceWidth);
            //计算高度的缩放比例
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;
            //期望的宽度
            int destWidth = (int)(sourceWidth * nPercent);
            //期望的高度
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((System.Drawing.Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //绘制图像
            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();
            imgToResize.Dispose();
            return (System.Drawing.Image)b;
        }

        //拼图函数
        public static void MergeImage(List<MergeImg> mapLists, string saveFile, string desc)
        {
            if (mapLists == null && mapLists.Count <= 0)
            {
                return;
            }

            // 图片集合
            Bitmap[] maps = new Bitmap[mapLists.Count];

            for (int i = 0; i < mapLists.Count; i++)
            { //读取本地图片初始化Bitmap
                Bitmap map = null;

                //第一个图片对象,背景图片
                map = new Bitmap(mapLists[i].Path);
                maps[i] = map;
            }


            // 初始化背景图片的宽高
            Bitmap bitMap = new Bitmap(mapLists[0].Width, mapLists[0].Height);
            // 初始化画板
            Graphics g1 = Graphics.FromImage(bitMap);

            for (int k = 0; k < maps.Length; k++)
            {
                //绘制图片,背景图
                for (int i = 0; i < maps[k].Width; i++)
                {
                    for (int j = 0; j < maps[k].Height; j++)
                    {
                        // 以像素点形式绘制(将要拼图的图片上的每个坐标点绘制到拼图对象的指定位置,直至所有点都绘制完成)
                        var temp = maps[k].GetPixel(i, j);

                        //透明背景跳过
                        if (temp.A == 0 && temp.R == 0 && temp.G == 0 && temp.B == 0) { continue; }

                        // 将图片画布的点绘制到整体画布的指定位置
                        bitMap.SetPixel(mapLists[k].LeftWidth + i, mapLists[k].TopHeight + j, temp);
                    }
                }
                maps[k].Dispose();
            }
            //写入文字
            for (int i = 0; i < (desc.Length / 10 + 1); i++)
            {
                int length = 10;
                if (i == desc.Length / 10)
                {
                    length = desc.Length - 10 * i;
                }
                if (length > 0)
                {
                    g1.DrawString(desc.Substring(10 * i, length),
                                     new Font(FontFamily.GenericSansSerif, 10f),
                                     Brushes.Black, new PointF(20, 240 + i * 25)); //可自定义高度
                }
            }

            // 保存输出到本地
            bitMap.Save(saveFile, ImageFormat.Png);
            g1.Dispose();
            bitMap.Dispose();
        }


    }


    /// <summary>
    /// 合并图片类
    /// </summary>
    public class MergeImg
    {
        /// <summary>
        /// 图片路径
        /// </summary>
        public string Path { get; set; }
        /// <summary>
        /// 左边距
        /// </summary>
        public int LeftWidth { get; set; }
        /// <summary>
        /// 上边距
        /// </summary>
        public int TopHeight { get; set; }
        /// <summary>
        /// 宽度
        /// </summary>
        public int Width { get; set; }
        /// <summary>
        /// 宽度
        /// </summary>
        public int Height { get; set; }


    }
        
        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

acycwf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值