問題
如何 文字轉PNG檔
解決方法
當遇到要將文字PNG的時候,可以使用這邊所提供的Function
這隻Function做到的是輸出最符合文字大小的PNG檔
private void DrawFont2PNG(String Font, int FontSize,String Path,String Name,String Text)
{
Bitmap newBitmap = null;
Graphics g = null;
//Font Example:”新細明體”
Font fontCounter = new Font(Font,FontSize));
// calculate size of the string.
newBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; SizeF stringSize = g.MeasureString(Text, fontCounter);
int nWidth = (int)stringSize.Width;
int nHeight = (int)stringSize.Height;
g.Dispose();
newBitmap.Dispose();
newBitmap = new Bitmap(nWidth, nHeight, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.White),new Rectangle(0, 0, nWidth, nHeight));
g.DrawString(Text ,fontCounter,new SolidBrush(Color.Black), 0, 0);
newBitmap.MakeTransparent(Color.White);
newBitmap.Save(Path+Name+".png", ImageFormat.Png);
}
沒有留言:
張貼留言