public static Image createLongTwitImage(TextBox tbWeibo) { //获取图片长度 Graphics g = Graphics.FromHwnd(tbWeibo.Handle); int tmptbHeight; try { tmptbHeight = (int)g.MeasureString(tbWeibo.Text + "\r\n\r\n\t\t\tBy #SinaWeiboClient#", tbWeibo.Font, 440).Height; } catch { return null; } finally { g.Dispose(); } //生成文本框 TextBox tmptb = new TextBox(); tmptb.Multiline = true; tmptb.Width = 440; tmptb.Height = tmptbHeight; tmptb.Text = tbWeibo.Text + "\r\n\r\n\t\t\tBy #SinaWeiboClient#"; tmptb.BackColor = Color.White; tmptb.BorderStyle = BorderStyle.None; //转化为图片 Bitmap bmp = new Bitmap(tmptb.Width, tmptb.Height); Rectangle rect = new Rectangle(new Point(0, 0), tmptb.Size); tmptb.DrawToBitmap(bmp, rect); tmptb.Dispose(); //MeasureString方法不甚精准,消除底部空白 int lines = 0, pixels = 0, white = Color.White.ToArgb(); for (int i = bmp.Height; i > 0; i--, pixels = 0) { for (int j = 0; j < bmp.Width; j++) { if (bmp.GetPixel(j, i).ToArgb() == white) pixels++; else pixels = 0; } if (pixels == bmp.Width) lines++; else break; } return lines > 0 ? bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height - lines), System.Drawing.Imaging.PixelFormat.Format24bppRgb) : bmp; }
//返回转换的点阵数据 private static byte[][] TAC_TurnTextBoxToPix(TextBox tbFont) { int nWidth = tbFont.Width; int nHeight = tbFont.Height; Bitmap bmpTbFont = new Bitmap(nWidth, nHeight); Rectangle rctTbFont = new Rectangle(0, 0, nWidth, nHeight); tbFont.DrawToBitmap(bmpTbFont, rctTbFont); byte[][] tempDesData = new byte[nHeight][]; Color coTemp = Color.Empty; for (int i = 0; i < nHeight; i++) { tempDesData[i] = new byte[nWidth]; for (int j = 0; j < nWidth; j++) { Color coRealPixel = Color.FromArgb(bmpTbFont.GetPixel(j, i).ToArgb() ^ 0xffffff); tempDesData[i][j] = TAC_IsBlack(coRealPixel, 1); } } return tempDesData; }