问题描述
- C# 生成文字水印在图片上
-
CS代码:public class CreatShui { public CreatShui() { // // TODO: Add constructor logic here // this.Width = 270; this.Height = 30; this.FontFamily = "幼圆"; this.FontSize = 12; this.FontStyle = FontStyle.Regular; this.Adaptable = true; this.Shadow = false; this.Left = 0; this.Top = 0; this.Alpha = 255; this.Red = 0; this.Green = 0; this.Blue = 0; this.BackgroundImage = ""; this.Quality = 100; this.BgColor = Color.FromArgb(0, 255, 255, 255); } /**/ /// <summary> /// 字体 /// </summary> public string FontFamily { get; set; } /**/ /// <summary> /// 文字大小 /// </summary> public int FontSize { get; set; } /**/ /// <summary> /// 文字风格 /// </summary> public FontStyle FontStyle { get; set; } /**/ /// <summary> /// 透明度0-255,255表示不透明 /// </summary> public int Alpha { get; set; } /**/ /// <summary> /// 水印文字是否使用阴影 /// </summary> public bool Shadow { get; set; } public int Red { get; set; } public int Green { get; set; } public int Blue { get; set; } /**/ /// <summary> /// 底图 /// </summary> public string BackgroundImage { get; set; } /**/ /// <summary> /// 水印文字的左边距 /// </summary> public int Left { get; set; } /**/ /// <summary> /// 水印文字的顶边距 /// </summary> public int Top { get; set; } /**/ /// <summary> /// 生成后的图片 /// </summary> public string ResultImage { get; set; } /**/ /// <summary> /// 水印文本 /// </summary> public string Text { get; set; } /**/ /// <summary> /// 生成图片的宽度 /// </summary> public int Width { get; set; } /**/ /// <summary> /// 生成图片的高度 /// </summary> public int Height { get; set; } /**/ /// <summary> /// 若文字太大,是否根据背景图来调整文字大小,默认为适应 /// </summary> public bool Adaptable { get; set; } public Color BgColor { get; set; } /**/ /// <summary> /// 输出图片质量,质量范围0-100,类型为long /// </summary> public long Quality { get; set; } /**/ /// <summary> /// 立即生成水印效果图 /// </summary> /// <returns>生成成功返回true,否则返回false</returns> public bool Create() { try { Bitmap bitmap; Graphics g; //使用纯背景色 if (this.BackgroundImage.Trim() == "") { bitmap = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);//.Format64bppArgb); g = Graphics.FromImage(bitmap); g.Clear(this.BgColor); } else { bitmap = new Bitmap(Image.FromFile(this.BackgroundImage)); g = Graphics.FromImage(bitmap); } g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; Font f = new Font(this.FontFamily, this.FontSize, this.FontStyle); SizeF size = g.MeasureString(this.Text, f); // 调整文字大小直到能适应图片尺寸 while (this.Adaptable == true && size.Width > bitmap.Width) { this.FontSize--; f = new Font(this.FontFamily, this.FontSize, this.FontStyle); size = g.MeasureString(this.Text, f); } Brush b = new SolidBrush(Color.FromArgb(this.Alpha, this.Red, this.Green, this.Blue)); StringFormat strFormat = new StringFormat(); strFormat.Alignment = StringAlignment.Near; if (this.Shadow) { Brush b2 = new SolidBrush(Color.FromArgb(90, 0, 0, 0)); g.DrawString(this.Text, f, b2, this.Left + 2, this.Top + 1); } g.DrawString(this.Text, f, b, new PointF(this.Left, this.Top), strFormat); bitmap.Save(this.ResultImage, ImageFormat.Png); //MemoryStream ms = new MemoryStream(); ////保存为Jpg类型 //bitmap.Save(ms, ImageFormat.Png); //Image img = Image.FromStream(ms); ////bitmap.Save(ms, ImageFormat.Png); //img.Save(this.ResultImage, ImageFormat.Png); //img.Dispose(); bitmap.Dispose(); g.Dispose(); return true; } catch { return false; } } }
调用方式:
Core.CreatShui cs = new Core.CreatShui(); cs.Text = "我是中国人"; cs.Create();
时间: 2024-12-21 22:11:53