输出一张自定义文字的图片

  /// <summary>
        /// 返回图片
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>

        protected FileContentResult RelsultBitmap(Bitmap img)
        {
            using (Bitmap bt = img)
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    bt.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                    return File(ms.ToArray(), "FrameImages/Gif");
                }
            }
        }


        /// <summary>
        /// 提示图片返回
        /// </summary>
        /// <param name="keyWord">提示文字</param>
        protected FileContentResult TipsImage(string keyWord)
        {
            int fontSize = 15;
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            using (Bitmap img = new Bitmap(288, 288))
            {
                float y = img.Height / (keyWord.Length + 1);
                using (Graphics g = Graphics.FromImage(img))
                {
                    g.Clear(Color.White);//绘画背景颜色   
                    for (int i = 0; i < keyWord.Length; i++)
                    {
                        // 绘画文字   
                        g.DrawString(keyWord[i].ToString(), new Font("Arial", fontSize, FontStyle.Regular), Brushes.Red, (img.Width / 2) - (fontSize / 2), y * (i + 1), sf);
                    }
                    return RelsultBitmap(img);
                }
            }
        }

效果图:

  

猜你喜欢

转载自www.cnblogs.com/5tomorrow/p/11249355.html
今日推荐