参考http://www.bmestu.com/studynewstail.asp?Stnew_ID=756作小许改动
1
public static void AddSignPic(System.Drawing.Image img, string FileName, string SignFileName, int x, int y, int Quality, int SignTransparency)2

{3
Graphics g = Graphics.FromImage(img);4
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;5
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;6
System.Drawing.Image Sign = new Bitmap(SignFileName);7

8
if (Sign.Height >= img.Height || Sign.Width >= img.Width)9

{10
return;11
}12
ImageAttributes imageAttributes = new ImageAttributes();13
ColorMap colorMap = new ColorMap();14
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);15
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);16

ColorMap[] remapTable =
{ colorMap };17

18
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);19

20
float transparency = 0.5F;21
if (SignTransparency >= 1 && SignTransparency <= 10)22

{23
transparency = (SignTransparency / 10.0F);24
}25

if (SignTransparency == 100)
{26
transparency = 0.0F;27
}28

if (SignTransparency == 99)
{29
transparency = 100.0F;30
}31

float[][] colorMatrixElements =
{32

new float[]
{1.0f,0.0f,0.0f,0.0f,0.0f},33

new float[]
{0.0f,1.0f,0.0f,0.0f,0.0f},34

new float[]
{0.0f,0.0f,1.0f,0.0f,0.0f},35

new float[]
{0.0f,0.0f,0.0f,transparency,0.0f},36

new float[]
{0.0f,0.0f,1.0f,0.0f,0.0f}37
};38

39
ColorMatrix colormatrix = new ColorMatrix(colorMatrixElements);40
imageAttributes.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);41
int xpos = x;42
int ypos = y;43
g.DrawImage(Sign, new Rectangle(xpos, ypos, Sign.Width, Sign.Height), 0, 0, Sign.Width, Sign.Height, GraphicsUnit.Pixel, imageAttributes);44
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();45
ImageCodecInfo ici = null;46
foreach (ImageCodecInfo codec in codecs)47

{48
if (codec.MimeType.IndexOf("jpeg") > 1)49

{50
ici = codec;51
}52
}53
EncoderParameters encoderParams = new EncoderParameters();54
long[] qualityParam = new long[1];55
if (Quality < 0 || Quality > 100)56

{57
Quality = 80;58
}59
qualityParam[0] = Quality;60

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

64
if (ici != null)65

{66
img.Save(FileName, ici, encoderParams);67
}68
else69

{70
img.Save(FileName);71
}72

73
g.Dispose();74
img.Dispose();75
Sign.Dispose();76
imageAttributes.Dispose();77

78

79
}
根据每个人需要生成的图片不同,需要作相应调整,以下代码生成图片如

1
public static void BuildDatePic(DateTime dt)2

{3

4
5
string y = dt.ToString("yyyy");6
string m = dt.ToString("MM");7
string d = dt.ToString("dd");8

9
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath("/img/" + y + m + d + ".jpg")))10

{11
System.Drawing.Image img = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("publicimg/bg.jpg"));12

13

14
string filename = System.Web.HttpContext.Current.Server.MapPath("img/test1.jpg");15
string watername = System.Web.HttpContext.Current.Server.MapPath("publicimg/" + y + ".jpg");16
ArticleService.AddSignPic(img, filename, watername, 10, 5, 99, 99);17

18
img = System.Drawing.Image.FromFile(filename);19

20
string filename2 = System.Web.HttpContext.Current.Server.MapPath("img/test2.jpg");21
watername = System.Web.HttpContext.Current.Server.MapPath("publicimg/" + m + ".jpg");22
ArticleService.AddSignPic(img, filename2, watername, 39, 13, 99, 6);23

24
img = System.Drawing.Image.FromFile(filename2);25

26
string filename3 = System.Web.HttpContext.Current.Server.MapPath("img/test3.jpg");27
watername = System.Web.HttpContext.Current.Server.MapPath("publicimg/white_ball.jpg");28
ArticleService.AddSignPic(img, filename3, watername, 7, 28, 99, 99);29

30
img = System.Drawing.Image.FromFile(filename3);31
string filename4 = System.Web.HttpContext.Current.Server.MapPath("img/test4.jpg");32
watername = System.Web.HttpContext.Current.Server.MapPath("publicimg/" + d + "D.jpg");33
ArticleService.AddSignPic(img, filename4, watername, 18, 31, 99, 99);34

35
if (File.Exists(filename))36

{37
File.Delete(filename);38
}39
if (File.Exists(filename2))40

{41
File.Delete(filename2);42
}43
if (File.Exists(filename3))44

{45
File.Delete(filename3);46
}47
if (File.Exists(filename4))48

{49
File.Copy(filename4, System.Web.HttpContext.Current.Server.MapPath("/img/" + y + m + d + ".jpg"));50
File.Delete(filename4);51
}52
}53
}
页面调用
BuildDatePic(DateTime.Now);
本人是用在个人blog里写入日记即生成当日图片,也许能在其它一些方便有作用,更希望能举一反三,有更好的方案出现。
PS:日期可以不用图片,直接用文字生成在原图上,下次改进。这方面知识还比较缺乏,希望有人能指点一二,或者提供个实例看看。
浙公网安备 33010602011771号