生成长微博(文转图)方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fengyarongaa/article/details/8595315

 #region 长微博生成图片处理合心方法
        public string ConvertTextFileToImage(String text, String imageFile)
        {
            System.Drawing.Font drawFont = new System.Drawing.Font("微软雅黑", 12);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(1, 1);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            //String text = System.IO.File.ReadAllText(textFile, Encoding.GetEncoding("GB2312"));
            System.Drawing.SizeF sf = g.MeasureString(text, drawFont, 440); //设置一个显示的宽度 
            string logo = System.Web.HttpContext.Current.Server.MapPath("~/images/logo.png");
            System.Drawing.Image watering = System.Drawing.Image.FromFile(logo);//得到数字水印图片  
            image = new System.Drawing.Bitmap(image, new System.Drawing.Size(Convert.ToInt32(sf.Width + 10), Convert.ToInt32(sf.Height + 140)));
            g = System.Drawing.Graphics.FromImage(image);
            g.Clear(System.Drawing.Color.White);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.DrawString("长微博", new System.Drawing.Font("微软雅黑", 15, FontStyle.Bold), new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(150, 150, 150)), new System.Drawing.RectangleF(new System.Drawing.PointF(10, 25), sf));
            g.DrawImage(watering, new Rectangle(292, 10, watering.Width, watering.Height), 0, 0, watering.Width, watering.Height, GraphicsUnit.Pixel);
            g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Gray), new System.Drawing.Point(10, 58), new System.Drawing.Point(image.Width - 10, 58));
            g.DrawString(text, drawFont, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(new System.Drawing.PointF(10, 70), sf));//设置在画板上的坐标
            g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Gray), new System.Drawing.Point(10, image.Height - 55), new System.Drawing.Point(image.Width - 10, image.Height - 55));
            g.DrawString("来自博长微博工具", new System.Drawing.Font("宋体", 10), new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(150, 150, 150)), new System.Drawing.RectangleF(new System.Drawing.PointF(10, image.Height - 45), sf));
            g.DrawString("图文自动生成,不代表站方观点", new System.Drawing.Font("宋体", 10), new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(150, 150, 150)), new System.Drawing.RectangleF(new System.Drawing.PointF(10, image.Height - 25), sf));
            string urladdress = System.Web.HttpContext.Current.Server.MapPath("\\UserUploads\\Images\\");
            //image.Save(urladdress + imageFile, System.Drawing.Imaging.ImageFormat.Png);
            string im = ImgInBank(image, imageFile);
            g.Dispose();
            image.Dispose();
            return im;
        }
        #endregion


长微博的地方用得到,logo.png是网站额Logo。最开始用的复杂方法,后来发现效果不太好,后来演化成这样。


阅读更多

没有更多推荐了,返回首页