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

      

  • 相关阅读:
    Codeforces Gym
    洛谷试炼场一句话题解
    优先队列板子
    BZOJ 3524 [POI2014] Couriers/洛谷3567(可持久化线段树)
    hdu 4417 Super Mario(可持久化线段树)
    poj 2559 Largest Rectangle in a Histogram (单调栈)
    zoj 4019 Schrödinger's Knapsack
    hdu 1521 排列组合(指数型生成函数板子)
    hdu 2072 单词数(普通型生成函数板子)
    RabbitMQ广播:direct模式
  • 原文地址:https://www.cnblogs.com/wuguangwei/p/14387902.html
Copyright © 2011-2022 走看看