引言
基于生成圖片實(shí)現(xiàn)了一個手機(jī)號轉(zhuǎn)圖片的需求。 內(nèi)容也很簡單,直接用手機(jī)號生成一個png圖片。就是為了背景透明以便其他地方調(diào)用。 有無鋸齒主要依靠一句代碼:g.TextRenderingHint= TextRenderingHint.AntiAlias;
生成圖片
1、有鋸齒
	
2、無鋸齒
	
生成方法
string color = "#ff6633";     System.Drawing.Bitmap image = new System.Drawing.Bitmap(170, 35);    Graphics g = Graphics.FromImage(image);    try    {      g.TextRenderingHint= TextRenderingHint.AntiAlias; //消除鋸齒            //生成隨機(jī)生成器      Random random = new Random();     //清空圖片背景色      //g.Clear(Color.Transparent);      //畫圖片的背景噪音線      /*for (int i = 0; i < 2; i++)      {        int x1 = random.Next(image.Width);        int x2 = random.Next(image.Width);        int y1 = random.Next(image.Height);        int y2 = random.Next(image.Height);        g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);      }      */      System.Drawing.ColorConverter colConvert = new System.Drawing.ColorConverter();      Color fontColor =(System.Drawing.Color)colConvert.ConvertFromString(color);      Font font = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Bold);      LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), fontColor, fontColor,LinearGradientMode.Horizontal);      g.DrawString(phone, font, brush, 2, 2);      //畫圖片的前景噪音點(diǎn)       //for (int i = 0; i < 50; i++)      //{      //  int x = random.Next(image.Width);      //  int y = random.Next(image.Height);      //  image.SetPixel(x, y, Color.FromArgb(random.Next()));      //}       //畫圖片的邊框線      //g.DrawRectangle(new Pen(Color.White), 0, 0, image.Width - 1, image.Height - 1);       System.IO.MemoryStream ms = new System.IO.MemoryStream();      Color backColor = image.GetPixel(1, 1);      image.MakeTransparent(backColor);      image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);      context.Response.ClearContent();      context.Response.ContentType = "image/x-png";      context.Response.BinaryWrite(ms.ToArray());    }    finally    {      g.Dispose();      image.Dispose();    }參考資料
http://www.blue1000.com/bkhtml/c17/2013-03/71115.htm
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選