• 为指定的背景图片按比例位置添加小程序二维码,并且在指定背景图片绘制相应的文字。(转载请注明来源)


     /// <summary>
        /// 图像生成辅助类
        /// </summary>
        public class ImageHelper
        {
    
            public static Bitmap GetDeskBitMap(string url, string name, StringBuilder sb)
            {
                try
                {
                    var xiaochengxuBitmap = UrlToImage(url);
                    FontFamily fontFamily = new FontFamily("微软雅黑");
                    Font font = new Font(fontFamily, 40);
                    System.Drawing.Bitmap backgroundImage = Resource.desk;
                    var res = DrawText(backgroundImage, xiaochengxuBitmap, name, font, Color.White, 260);
                    return res;
                }
                catch (Exception ex)
                {
                    sb.Append(ex.ToString());
                    return null;
                }
            }
    
            public static Bitmap UrlToImage(string url)
            {
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                using (MemoryStream ms = new MemoryStream(Bytes))
                {
                    Image tTempBitmap = Image.FromStream(ms);
                    return (Bitmap)tTempBitmap;
                }
            }
            public static Bitmap DrawText(Bitmap original, Bitmap erweima, string text, Font font, Color color, int height)
            {
                Bitmap result = new Bitmap(original.Width, original.Height);
                using (Graphics graphics = Graphics.FromImage(result))
                {
                    graphics.DrawImage(original, 0, 0);
                    int x = (original.Width - erweima.Width) / 2;
                    int y = (int)original.Height * 1 / 7 + (original.Height * 5 / 6 - erweima.Height) / 2;
                    graphics.DrawImage(erweima, new Rectangle(x, y, erweima.Width, erweima.Height));
                    Rectangle rect = new Rectangle(0, original.Height - height, original.Width, height);
                    using (SolidBrush brush = new SolidBrush(color))
                    {
                        using (StringFormat format = new StringFormat())
                        {
                            format.Alignment = StringAlignment.Center;
                            format.LineAlignment = StringAlignment.Center;
                            graphics.DrawString(text, font, brush, rect, format);
                        }
                    }
                }
                return result;
            }
    
        }
    

      

  • 相关阅读:
    系统架构师学习笔记_第十一章(下)
    通过IronRuby和C#学习RUBY系列[0]
    TIPS to comment your code
    超级简单:DIV布局
    js 查询XML数据添加到HTML select控件
    《架构之美》读书笔记(一)
    18个不常见的C#关键字,您使用过几个?
    如何成为人尽皆知的C#开发人员
    实现Visual Studio 2010一个很简单的很酷的扩展
    一种简单的直观的高效的权限设计
  • 原文地址:https://www.cnblogs.com/wuguangwei/p/14387902.html
Copyright © 2020-2023  润新知