c# 合并圖片 image

來源:互聯網
上載者:User

如下是自己曾經編寫過的代碼,放到這個地方,免的以後自己在去查看怎麼編寫這樣的代碼.....

1:圖片上寫字,並設定背景色

 #region 建立樹節點的表徵圖
        /// <summary>
        /// 建立樹節點的表徵圖
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="txtColor"></param>
        /// <returns></returns>
        private Bitmap CreateNodeImg(string txt, Color txtColor)
        {
            if (txtColor == Color.Transparent)
                txtColor = Color.Black;
            Bitmap newBitMap = new Bitmap(12, 14);
            Graphics g = Graphics.FromImage(newBitMap);
            if (txtColor != Color.Black)
            {
                g.Clear(txtColor);//背景色
            }
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            FontFamily fm = new FontFamily("Arial");
            Font font = new Font(fm, 12, FontStyle.Regular, GraphicsUnit.Pixel);
            SolidBrush sb = new SolidBrush(Color.Black);

            g.DrawString(txt, font, sb, new PointF(0, 0));
            g.Dispose();
            return newBitMap;
        }

 

2 合并圖片

#region 合并圖片
        /// <summary>
        /// 合并圖片
        /// </summary>
        /// <param name="maps"></param>
        /// <returns></returns>
        private Bitmap MergerImg(params Bitmap[] maps)
        {
            int i = maps.Length;
            if (i == 0)
                throw new Exception("圖片數不能夠為0");

            //建立要顯示的圖片對象,根據參數的個數設定寬度
            Bitmap backgroudImg = new Bitmap(i * 12, 16);
            Graphics g = Graphics.FromImage(backgroudImg);

            //清除畫布,背景設定為白色
            g.Clear(System.Drawing.Color.White);

            for (int j = 0; j < i; j++)
            {
                g.DrawImage(maps[j], j * 11, 0, maps[j].Width, maps[j].Height);
            }
            g.Dispose();
            return backgroudImg;
        }
        #endregion

        #region 合并圖片
        /// <summary>
        /// 合并圖片
        /// </summary>
        /// <param name="bitMapDic"></param>
        /// <returns></returns>
        private Bitmap MergerImg(Dictionary<string, Bitmap> bitMapDic)
        {
            if (bitMapDic == null || bitMapDic.Count == 0)
                throw new Exception("圖片數不能夠為0");
            //建立要顯示的圖片對象,根據參數的個數設定寬度
            Bitmap backgroudImg = new Bitmap(bitMapDic.Count * 12, 16);
            Graphics g = Graphics.FromImage(backgroudImg);

            //清除畫布,背景設定為白色
            g.Clear(System.Drawing.Color.White);

            int j = 0;
            foreach (KeyValuePair<string, Bitmap> entry in bitMapDic)
            {
                Bitmap map = entry.Value;
                g.DrawImage(map, j * 11, 0, map.Width, map.Height);
                j++;
            }
            g.Dispose();
            return backgroudImg;
        }

 

合并圖片還可以:

 //int i = maps.Length;
            //if (i == 0)
            //    throw new Exception("圖片數不能夠為0");

            ////建立要顯示的圖片對象,根據參數的個數設定寬度
            //Bitmap backgroudImg = new Bitmap(i * 16, 16);
            //Graphics g = Graphics.FromImage(backgroudImg);

            ////清除畫布,背景設定為白色
            //g.Clear(System.Drawing.Color.White);

            //g.DrawImageUnscaled(maps[0], 0, 0);
            //g.DrawImageUnscaled(maps[1], maps[0].Width, 0);

            //g.Dispose();
            //return backgroudImg;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.