zoukankan      html  css  js  c++  java
  • C#图片添加水印

    1.图片水印封装方法     

    /// <summary>
    /// 图片水印
    /// </summary>
    /// <param name="imgPath">服务器图片相对路径</param>
    /// <param name="filename">保存文件名</param>
    /// <param name="watermarkFilename">水印文件相对路径</param>
    /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下 10=底部</param>
    /// <param name="quality">附加水印图片质量,0-100</param>
    /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
    public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
    {
    if (!File.Exists(GetMapPath(imgPath)))
    return;
    byte[] _ImageBytes = File.ReadAllBytes(GetMapPath(imgPath));
    Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
    filename = GetMapPath(filename); 

    if (watermarkFilename.StartsWith("/") == false)
    watermarkFilename = "/" + watermarkFilename;
    watermarkFilename = GetMapPath(watermarkFilename);
    if (!File.Exists(watermarkFilename))
    return;
    Graphics g = Graphics.FromImage(img);
    //设置高质量插值法
    //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //设置高质量,低速度呈现平滑程度
    //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    Image watermark = new Bitmap(watermarkFilename); 

    ImageAttributes imageAttributes = new ImageAttributes();
    ColorMap colorMap = new ColorMap(); 

    colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
    colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
    ColorMap[] remapTable = { colorMap }; 

    imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

     float transparency = 0.5F;

    if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
    transparency = (watermarkTransparency / 10.0F); 

    float[][] colorMatrixElements = {
    new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
    new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
    new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
    }; 

    ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); 

    imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 

    int xpos = 0;
    int ypos = 0; 

    switch (watermarkStatus)
    {
    case 1:
    xpos = (int)(img.Width * (float).01);
    ypos = (int)(img.Height * (float).01);
    break;
    case 2:
    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
    ypos = (int)(img.Height * (float).01);
    break;
    case 3:
    xpos = (int)((img.Width * (float).99) - (watermark.Width));
    ypos = (int)(img.Height * (float).01);
    break;
    case 4:
    xpos = (int)(img.Width * (float).01);
    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
    break;
    case 5:
    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
    break;
    case 6:
    xpos = (int)((img.Width * (float).99) - (watermark.Width));
    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
    break;
    case 7:
    xpos = (int)(img.Width * (float).01);
    ypos = (int)((img.Height * (float).99) - watermark.Height);
    break;
    case 8:
    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
    ypos = (int)((img.Height * (float).99) - watermark.Height);
    break;
    case 9:
    xpos = (int)((img.Width * (float).99) - (watermark.Width));
    ypos = (int)((img.Height * (float).99) - watermark.Height);
    break;
    case 10:
    ypos = (int)((img.Height * (float).99) - watermark.Height)+10;
    break;

    g.DrawImage(watermark, new Rectangle(1, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

    ImageCodecInfo ici = null;
    foreach (ImageCodecInfo codec in codecs)
    {
    if (codec.MimeType.IndexOf("jpeg") > -1)
    ici = codec;
    }
    EncoderParameters encoderParams = new EncoderParameters();
    long[] qualityParam = new long[1];
    if (quality < 0 || quality > 100)
    quality = 80; 

    qualityParam[0] = quality; 

    EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
    encoderParams.Param[0] = encoderParam; 

    if (ici != null)
    img.Save(filename, ici, encoderParams);
    else
    img.Save(filename); 

    g.Dispose();
    img.Dispose();
    watermark.Dispose();
    imageAttributes.Dispose();
    }

    2.调用方法

    AddImageSignPic(@"~UploadsWaterFace水印_sl.jpg", @"~UploadsWaterFace水印_waternew.jpg", @"UploadsWaterFacemodel_1.png", 10, 100, 10)

  • 相关阅读:
    【LOJ #3058】「HNOI2019」白兔之舞(单位根反演+矩阵快速幂+MTT)
    【LOJ #2289】「THUWC 2017」在美妙的数学王国中畅游(LCT+泰勒展开)
    【LOJ #3193】「ROI 2019 Day2」机器人高尔夫球赛(DP+Map)
    【Codeforces 1119H】Triple(FWT)
    PKUWC2020 (旅)游记
    多项式算法合集
    redis入门学习
    servelt
    spring容器原理学习
    Spring MVC
  • 原文地址:https://www.cnblogs.com/dsjbk/p/12660898.html
Copyright © 2011-2022 走看看