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

     /// <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;
            }
    
        }
    

      

  • 相关阅读:
    4.9 省选模拟赛 圆圈游戏 树形dp set优化建图
    C#异步编程の-------异步编程模型(APM)
    C#异步编程の----Threadpool( 线程池)
    C#の----Func,Action,predicate在WPF中的应用
    C#常见委托のdelegate定义,Func,Action,Predicate总结
    c++のmap的遍历
    C#深度学习の----深拷贝与浅拷贝
    NSIS学习记录の----查找注册表某个键是否存在
    WPFの操作文件浏览框几种方式
    C#Url处理类
  • 原文地址:https://www.cnblogs.com/wuguangwei/p/14387902.html
Copyright © 2011-2022 走看看