主è¦æ¯è¦å°ä¸ä¸ªæ é¢åå 段æåç»å¶å°åºå®å¤§å°çå¾çä¸ï¼å¦æä¸å¼ æ¾ä¸ä¸å°±çæå¤å¼ ãå¨ä½¿ç¨DrawStringæ¯åç°æ æ³æ§å¶è¡è·
namespace TibetTest { public class Utils { // } public class ConvertImage { public bool TextToImage(string title_Text, string title_FontFamily, float title_FontEmSize, FontStyle title_FontStyle, Color title_Color, int title_MarginTop, int title_MarginBottom, string content_Text, string content_FontFamily, float content_FontEmSize, FontStyle content_FontStyle, Color content_Color, int content_RowSpacing, int content_WordSpacing, int page_Width, int page_Height, int page_MarginLeftRight, int page_MarginTop, Color background_Color, string SavePath) { //TODO:æ£æµå¹¶ä¿®æ£åæ° System.Drawing.Bitmap bit = new System.Drawing.Bitmap(page_Width, page_Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bit); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; g.Clear(background_Color); //--åæ é¢--æ¬ä»£ç åå®æ é¢å
容ä¸ä¼è¶
åºä¸é¡µ Font title_font = new Font(title_FontFamily, title_FontEmSize, title_FontStyle); Brush title_brush = new SolidBrush(title_Color); SizeF size = g.MeasureString(title_Text, title_font, page_Width - page_MarginLeftRight * 2); g.DrawString(title_Text, title_font, title_brush, new RectangleF(new PointF(page_MarginLeftRight, title_MarginTop), //æ é¢çä¸è¾¹è·åç¬æå® new SizeF(page_Width - page_MarginLeftRight * 2, page_Height - title_MarginTop))); //--计ç®å
容æ¯ä¸ªå符çæ¾ç¤ºä½ç½® Font font = new Font(content_FontFamily, content_FontEmSize, content_FontStyle); Brush brush = new SolidBrush(content_Color); List<PageClass> lpageClass = GetShowList(content_Text, g, font, page_Width, page_Height, content_RowSpacing, content_WordSpacing, size.Height + title_MarginTop + title_MarginBottom, page_MarginLeftRight, page_MarginTop); foreach (PageClass pc in lpageClass) { if (pc.pageindex > 1) { g.Dispose(); bit.Dispose(); bit = new System.Drawing.Bitmap(page_Width, page_Height); g = System.Drawing.Graphics.FromImage(bit); g.Clear(background_Color); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; } GDIMethods.DrawDriverString(g, pc.txt, font, brush, pc.pos); bit.RotateFlip(RotateFlipType.Rotate90FlipXY); bit.Save(string.Format(SavePath, pc.pageindex), System.Drawing.Imaging.ImageFormat.Bmp);
} return true;
} /// <summary> /// 计ç®å
容æ¯ä¸ªå符çæ¾ç¤ºä½ç½®ï¼æ ¹æ®å符大å°èªå¨åè¡ï¼æ¯æç©ºæ ¼ãæ¢è¡ã两边èªå¨å¯¹é½ /// </summary> /// <param name="txt">ææ¬å
容</param> /// <param name="g">ç»å¾åºå</param> /// <param name="font">åä½</param> /// <param name="pagewidth">页宽</param> /// <param name="pageheight">页é«</param> /// <param name="rowspace">è¡è·</param> /// <param name="wordspace">å符é´è·</param> /// <param name="firstPageMaginTop">é¦é¡µä¸è¾¹è·</param> /// <param name="marginleftright">页左å³è¾¹è·</param> /// <param name="margintop">æ®é页ä¸è¾¹è·</param> /// <returns></returns> public List<PageClass> GetShowList(string txt, Graphics g, Font font, int pagewidth, int pageheight, float rowspace, float wordspace, float firstPageMaginTop, int marginleftright, int margintop) { int pageindex = 1; float left = 0, top = firstPageMaginTop + font.SizeInPoints; ; float lastRight = marginleftright - wordspace; float lastRowBotton = top; float rowheight = font.SizeInPoints + rowspace; int colstart = 0, colend = 0; float colspace; string curTxt, measureStr; SizeF size; List<PointF> lpos = new List<PointF>(); List<PageClass> lpageClass = new List<PageClass>(); PageClass curPage = new PageClass(); System.Text.StringBuilder str = new System.Text.StringBuilder(); for (int i = 0; i < txt.Length; i++) //è¿ä¸ªåå¨è¿è¡æ¾ä¸ä¸ï¼è¦æ¾å°ä¸ä¸è¡ { curTxt = txt.Substring(i, 1); switch (curTxt) { case " ": measureStr = "2"; break; case "\t": measureStr = "2222"; break; case "ã": measureStr = "å½"; break; default: measureStr = curTxt; break; } size = g.MeasureString(measureStr, font, int.MaxValue, StringFormat.GenericTypographic); left = lastRight + wordspace; if ((left + size.Width) > pagewidth || curTxt == "\n") { if (curTxt != "\n") { colspace = (pagewidth - lastRight - marginleftright) / (colend - colstart); for (int j = colstart; j < colend; j++)//å¤çä¸¤è¾¹å¯¹é½ { lpos[j] = new PointF(lpos[j].X + colspace * (j - colstart + 1), lpos[j].Y); } } colstart = colend;
left = marginleftright; top = lastRowBotton + rowheight;
lastRowBotton = top; } colend++; lastRight = left + size.Width;
if (top > pageheight) //è¿è¡åºè¯¥æ¾å°ä¸ä¸é¡µäº { curPage.pageindex = pageindex; curPage.txt = str.ToString(); curPage.pos = lpos.ToArray(); lpageClass.Add(curPage);
pageindex++; curPage = new PageClass(); lpos.Clear(); //str.Clear(); str.Remove(0, str.Length); top = margintop + rowheight; lastRowBotton = top; colstart = 0; colend = 1; }
str.Append(curTxt); lpos.Add(new PointF(left, top));
} //ä¿åæåä¸é¡µ if (lpos.Count > 0) { curPage.pageindex = pageindex; curPage.txt = str.ToString(); curPage.pos = lpos.ToArray(); lpageClass.Add(curPage); } return lpageClass; } }
public class PageClass { public int pageindex; public string txt; public PointF[] pos; }
public class GDIMethods { private GDIMethods() { }
private enum DriverStringOptions { CmapLookup = 1, Vertical = 2, Advance = 4, LimitSubpixel = 8, }
public static void DrawDriverString(Graphics graphics, string text, Font font, Brush brush, PointF[] positions) { DrawDriverString(graphics, text, font, brush, positions, null); }
public static void DrawDriverString(Graphics graphics, string text, Font font, Brush brush, PointF[] positions, Matrix matrix) { if (graphics == null) throw new ArgumentNullException("graphics"); if (text == null) throw new ArgumentNullException("text"); if (font == null) throw new ArgumentNullException("font"); if (brush == null) throw new ArgumentNullException("brush"); if (positions == null) throw new ArgumentNullException("positions");
// Get hGraphics FieldInfo field = typeof(Graphics).GetField("nativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic); IntPtr hGraphics = (IntPtr)field.GetValue(graphics);
// Get hFont field = typeof(Font).GetField("nativeFont", BindingFlags.Instance | BindingFlags.NonPublic); IntPtr hFont = (IntPtr)field.GetValue(font);
// Get hBrush field = typeof(Brush).GetField("nativeBrush", BindingFlags.Instance | BindingFlags.NonPublic); IntPtr hBrush = (IntPtr)field.GetValue(brush);
// Get hMatrix IntPtr hMatrix = IntPtr.Zero; if (matrix != null) { field = typeof(Matrix).GetField("nativeMatrix", BindingFlags.Instance | BindingFlags.NonPublic); hMatrix = (IntPtr)field.GetValue(matrix); }
int result = GdipDrawDriverString(hGraphics, text, text.Length, hFont, hBrush, positions, (int)DriverStringOptions.CmapLookup, hMatrix); }
[DllImport("Gdiplus.dll", CharSet = CharSet.Unicode)] internal extern static int GdipMeasureDriverString(IntPtr graphics, string text, int length, IntPtr font, PointF[] positions, int flags, IntPtr matrix, ref RectangleF bounds);
[DllImport("Gdiplus.dll", CharSet = CharSet.Unicode)] internal extern static int GdipDrawDriverString(IntPtr graphics, string text, int length, IntPtr font, IntPtr brush, PointF[] positions, int flags, IntPtr matrix); }
}