// 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(); } } } }
// 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; 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; using (Bitmap bitmapTemp = new Bitmap(1, 1)) { 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)); 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; } } // 正式的图像 Bitmap bitmapDest = new Bitmap( source.Width, // (int)size.Width + 1, Math.Max(source.Height, y + max_height), // (int)size.Height + 1, PixelFormat.Format64bppPArgb); try { 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; // Color.FromArgb(128, 100, 100, 100) SolidBrush objBrush = new SolidBrush(info.colorText); // 透明颜色 ' Color.Black RectangleF rect = new RectangleF(x, y, size.Width, size.Height); if ((info.effect & ArtEffect.Shadow) == ArtEffect.Shadow) { SolidBrush 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(); /* stream = SaveGIFWithNewColorTable( bitmapDest, 256, true); */ 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 { bitmapDest.Dispose(); } }