c# 添加水印(強大的代碼)

添加水印的強大代碼,這是我從Discuz!Nt挖出來的代碼

功能很強大,編寫也很好,可以添加圖片水印和文字水印兩種

添加出來的效果非常好,現在就把代碼貼出來,供大家欣賞

 

 

 

要導入的空間:

 

Code
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

 

添加圖片水印的方法:

 

Code
/// <summary>
    
/// 加圖片水印
    
/// </summary>
    
/// <param name="filename">文件名</param>
    
/// <param name="watermarkFilename">水印文件名</param>
    
/// <param name="watermarkStatus">圖片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
    
/// <param name="quality">附加圖片質量,1是 0不是</param>
    
/// <param name="watermarkTransparency">水印的透明度 1--10 10爲不透明</param>
    public static void AddImageSignPic(Image img, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
    {
        Graphics g 
= Graphics.FromImage(img);
        
//設置高質量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        
//設置高質量,低速度呈現平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        Image watermark 
= new Bitmap(watermarkFilename);

        
if (watermark.Height >= img.Height || watermark.Width >= img.Width)
        {
            
return;
        }

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

        colorMap.OldColor 
= Color.FromArgb(25502550);
        colorMap.NewColor 
= Color.FromArgb(0000);
        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.0f0.0f},
                                                
new float[] {0.0f,  1.0f,  0.0f,  0.0f0.0f},
                                                
new float[] {0.0f,  0.0f,  1.0f,  0.0f0.0f},
                                                
new float[] {0.0f,  0.0f,  0.0f,  transparency, 0.0f},
                                                
new float[] {0.0f,  0.0f,  0.0f,  0.0f1.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;
        }

        g.DrawImage(watermark, 
new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 00, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
        
//g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel);

        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();
    }

 

下面這個是添加文字水印的方法

 

Code
 /// <summary>
    
/// 增加圖片文字水印
    
/// </summary>
    
/// <param name="filename">文件名</param>
    
/// <param name="watermarkText">水印文字</param>
    
/// <param name="watermarkStatus">圖片水印位置</param>
    public static void AddImageSignText(Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
    {
        
//System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img);
        
//    .FromFile(filename);
        Graphics g = Graphics.FromImage(img);
        Font drawFont 
= new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
        SizeF crSize;
        crSize 
= g.MeasureString(watermarkText, drawFont);

        
float xpos = 0;
        
float ypos = 0;

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

        
//            System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
        
//            StrFormat.Alignment = System.Drawing.StringAlignment.Center;
        
//
        
//            g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.White), xpos + 1, ypos + 1, StrFormat);
        
//            g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.Black), xpos, ypos, StrFormat);
        g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
        g.DrawString(watermarkText, drawFont, 
new SolidBrush(Color.Black), xpos, ypos);

        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();
        
//bmp.Dispose();
        img.Dispose();
    }

 

這個是我測試用的代碼

 

Code
 Image img = Image.FromStream(this.FileUpload1.PostedFile.InputStream);
        
string filename = Server.MapPath("img/test1.jpg");
        
string watername = Server.MapPath("img/11.jpg");
        WaterMark.AddImageSignPic(img, filename, watername, 
11006);

 

我的整個測試代碼在這裏,大家可以下載

水印源代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章