// 2016/10/5 改造为利用下级 Bitmap 函数 // parameters: // nWidth 控制折行的位置 public static MemoryStream BuildArtText( string strText, string strFontFace, float fFontSize, FontStyle fontstyle, Color colorText, Color colorBack, Color colorShadow, ArtEffect effect, ImageFormat imageformat, int nWidth = 500) { // 正式的图像 using (Bitmap bitmapDest = BuildArtText(strText, strFontFace, fFontSize, fontstyle, colorText, colorBack, colorShadow, effect, nWidth)) { MemoryStream stream = new MemoryStream(); if (imageformat == ImageFormat.Png && colorBack == Color.Transparent) { bitmapDest.MakeTransparent(colorBack); } if (imageformat == ImageFormat.Gif) { bitmapDest.MakeTransparent( colorBack); OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); quantizer.TransparentColor = colorBack; using (Bitmap quantized = quantizer.Quantize(bitmapDest)) { quantized.Save(stream, imageformat); } } else { bitmapDest.Save(stream, imageformat); // System.Drawing.Imaging.ImageFormat.Jpeg } return(stream); } }
// max_width 100% 50% // x "center" "0" "width" public static MemoryStream PaintText( string strSourceFileName, string strText, TextInfo info, string s_x, string s_y, string s_max_width, string s_max_height, ImageFormat imageformat) { SizeF size; using (Image source = Image.FromFile(strSourceFileName)) { int width = source.Width; int height = source.Height; int x = 0; if (s_x != "center") { x = GetValue(s_x, width, width, height); } int y = GetValue(s_y, height, width, height); int max_width = GetValue(s_max_width, width, width, height); int max_height = GetValue(s_max_height, height, width, height); Font font = null; try { using (Bitmap bitmapTemp = new Bitmap(1, 1)) { using (Graphics graphicsTemp = Graphics.FromImage(bitmapTemp)) { int text_height = (int)((float)max_height * 0.8); // 第一次测算,按照最大高度 font = new Font(info.FontFace, text_height, info.fontstyle, GraphicsUnit.Pixel); size = graphicsTemp.MeasureString( strText, font); int width_delta = (int)size.Width - max_width; if (width_delta > 0) { int nFontHeight = (int)((float)text_height * ((float)max_width / size.Width)); if (font != null) { font.Dispose(); } font = new Font(info.FontFace, nFontHeight, info.fontstyle, GraphicsUnit.Pixel); y += (text_height - nFontHeight) / 2; } if ((info.effect & ArtEffect.Shadow) == ArtEffect.Shadow) { size.Height += 2; size.Width += 2; } } } // 正式的图像 using (Bitmap bitmapDest = new Bitmap( source.Width, // (int)size.Width + 1, Math.Max(source.Height, y + max_height), // (int)size.Height + 1, PixelFormat.Format64bppPArgb)) { using (Graphics objGraphics = Graphics.FromImage(bitmapDest)) { objGraphics.Clear(info.colorBack);// Color.Transparent objGraphics.DrawImageUnscaled(source, new Point(0, 0)); // objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; // System.Drawing.Text.TextRenderingHint oldrenderhint = objGraphics.TextRenderingHint; //设置高质量,低速度呈现平滑程度 objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; StringFormat stringFormat = new StringFormat(); if (s_x == "center") { stringFormat.Alignment = StringAlignment.Center; x = 0; size.Width = source.Width; } else { stringFormat.Alignment = StringAlignment.Near; } using (Brush objBrush = new SolidBrush(info.colorText)) // 透明颜色 ' Color.Black { RectangleF rect = new RectangleF(x, y, size.Width, size.Height); if ((info.effect & ArtEffect.Shadow) == ArtEffect.Shadow) { using (Brush objBrushShadow = new SolidBrush(info.colorShadow)) { RectangleF rectShadow = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height); rectShadow.Offset(2, 2); objGraphics.DrawString(strText, font, objBrushShadow, rectShadow, stringFormat); } } objGraphics.DrawString(strText, font, objBrush, rect, stringFormat); } MemoryStream stream = new MemoryStream(); if (imageformat == ImageFormat.Gif) { bitmapDest.MakeTransparent( info.colorBack); OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); quantizer.TransparentColor = info.colorBack; using (Bitmap quantized = quantizer.Quantize(bitmapDest)) { quantized.Save(stream, imageformat); } } else { bitmapDest.Save(stream, imageformat); // System.Drawing.Imaging.ImageFormat.Jpeg } return(stream); } } } finally { if (font != null) { font.Dispose(); } } } }
// parameters: // nWidth 控制折行的位置 public static MemoryStream BuildArtText( string strText, string strFontFace, float fFontSize, FontStyle fontstyle, Color colorText, Color colorBack, Color colorShadow, ArtEffect effect, ImageFormat imageformat, int nWidth = 500) { SizeF size; using (Font font = new Font(strFontFace, fFontSize, fontstyle)) { using (Bitmap bitmapTemp = new Bitmap(1, 1)) { using (Graphics graphicsTemp = Graphics.FromImage(bitmapTemp)) { size = graphicsTemp.MeasureString( strText, font, nWidth); if ((effect & ArtEffect.Shadow) == ArtEffect.Shadow) { size.Height += 2; size.Width += 2; } } } // 正式的图像 using (Bitmap bitmapDest = new Bitmap((int)size.Width + 1, (int)size.Height + 1, PixelFormat.Format64bppPArgb)) { using (Graphics objGraphics = Graphics.FromImage(bitmapDest)) { // colorBack = Color.FromArgb(0, colorBack); objGraphics.Clear(colorBack);// Color.Transparent // objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; // System.Drawing.Text.TextRenderingHint oldrenderhint = objGraphics.TextRenderingHint; //设置高质量,低速度呈现平滑程度 objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Near; stringFormat.LineAlignment = StringAlignment.Center; // 2016/5/24 // Color.FromArgb(128, 100, 100, 100) using (Brush objBrush = new SolidBrush(colorText)) // 透明颜色 ' Color.Black { RectangleF rect = new RectangleF(0, 0, size.Width, size.Height); if ((effect & ArtEffect.Shadow) == ArtEffect.Shadow) { using (Brush objBrushShadow = new SolidBrush(colorShadow)) { RectangleF rectShadow = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height); rectShadow.Offset(2, 2); objGraphics.DrawString(strText, font, objBrushShadow, rectShadow, stringFormat); } } objGraphics.DrawString(strText, font, objBrush, rect, stringFormat); } } MemoryStream stream = new MemoryStream(); /* * stream = SaveGIFWithNewColorTable( * bitmapDest, * 256, * true); */ if (imageformat == ImageFormat.Png && colorBack == Color.Transparent) { bitmapDest.MakeTransparent(colorBack); } if (imageformat == ImageFormat.Gif) { bitmapDest.MakeTransparent( colorBack); OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); quantizer.TransparentColor = colorBack; using (Bitmap quantized = quantizer.Quantize(bitmapDest)) { quantized.Save(stream, imageformat); } } else { bitmapDest.Save(stream, imageformat); // System.Drawing.Imaging.ImageFormat.Jpeg } return(stream); } } }