2013年3月22日星期五

C# 给图片加图片或文字水印

C# 给图片加图片或文字水印

code1 图片加水印是我们做网站的经常使用的功能,下面的代码可以给图片加上图片水印或者文字水印,不过只是简单的实现,不能像专业软件水印效果那么好,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/// <summary>
/// 对一个指定的图片加上图片水印效果。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterImage">水印图片(Image对象)</param>
public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
{
    if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
    {
        return;
    }

    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);

    if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
    {
        return;
    }

    Graphics graphics = Graphics.FromImage(originalImage);

    int x = originalImage.Width - waterImage.Width - 10;
    int y = originalImage.Height - waterImage.Height - 10;
    int width = waterImage.Width;
    int height = waterImage.Height;

    graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
    graphics.Dispose();

    MemoryStream stream = new MemoryStream();
    originalImage.Save(stream, ImageFormat.Jpeg);
    originalImage.Dispose();

    System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);

    imageWithWater.Save(imageFile);
    imageWithWater.Dispose();
}

/// <summary>
/// 对一个指定的图片加上文字水印效果。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterText">水印文字内容</param>
public static void CreateTextWaterMark(string imageFile, string waterText)
{
    if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
    {
        return;
    }

    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);

    Graphics graphics = Graphics.FromImage(originalImage);

    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
    graphics.CompositingQuality = CompositingQuality.HighQuality;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

    SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
    Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
    SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);

    float x = (float)originalImage.Width - waterTextSize.Width - 10F;
    float y = (float)originalImage.Height - waterTextSize.Height - 10F;

    graphics.DrawString(waterText, waterTextFont, brush, x, y);

    graphics.Dispose();
    brush.Dispose();

    MemoryStream stream = new MemoryStream();
    originalImage.Save(stream, ImageFormat.Jpeg);
    originalImage.Dispose();

    System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);

    imageWithWater.Save(imageFile);
    imageWithWater.Dispose();
}

1 条评论: