屮艸芔茻2014 2014-02-17 14:05
浏览 2849

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();
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)