根据日期生成图片

 

参考http://www.bmestu.com/studynewstail.asp?Stnew_ID=756作小许改动

ContractedBlock.gif ExpandedBlockStart.gif Code
 1public static void AddSignPic(System.Drawing.Image img, string FileName, string SignFileName, int x, int y, int Quality, int SignTransparency)
 2ExpandedBlockStart.gifContractedBlock.gif        {
 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)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            {
10                return;
11            }

12            ImageAttributes imageAttributes = new ImageAttributes();
13            ColorMap colorMap = new ColorMap();
14            colorMap.OldColor = Color.FromArgb(25502550);
15            colorMap.NewColor = Color.FromArgb(0000);
16ExpandedSubBlockStart.gifContractedSubBlock.gif            ColorMap[] remapTable ={ colorMap };
17
18            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
19
20            float transparency = 0.5F;
21            if (SignTransparency >= 1 && SignTransparency <= 10)
22ExpandedSubBlockStart.gifContractedSubBlock.gif            {
23                transparency = (SignTransparency / 10.0F);
24            }

25ExpandedSubBlockStart.gifContractedSubBlock.gif            if (SignTransparency == 100{
26                transparency = 0.0F;
27            }

28ExpandedSubBlockStart.gifContractedSubBlock.gif            if (SignTransparency == 99{
29                transparency = 100.0F;
30            }

31ExpandedSubBlockStart.gifContractedSubBlock.gif            float[][] colorMatrixElements ={
32ExpandedSubBlockStart.gifContractedSubBlock.gif                new float[]{1.0f,0.0f,0.0f,0.0f,0.0f},
33ExpandedSubBlockStart.gifContractedSubBlock.gif                new float[]{0.0f,1.0f,0.0f,0.0f,0.0f},
34ExpandedSubBlockStart.gifContractedSubBlock.gif                new float[]{0.0f,0.0f,1.0f,0.0f,0.0f},
35ExpandedSubBlockStart.gifContractedSubBlock.gif                new float[]{0.0f,0.0f,0.0f,transparency,0.0f},
36ExpandedSubBlockStart.gifContractedSubBlock.gif                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), 00, Sign.Width, Sign.Height, GraphicsUnit.Pixel, imageAttributes);
44            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
45            ImageCodecInfo ici = null;
46            foreach (ImageCodecInfo codec in codecs)
47ExpandedSubBlockStart.gifContractedSubBlock.gif            {
48                if (codec.MimeType.IndexOf("jpeg"> 1)
49ExpandedSubBlockStart.gifContractedSubBlock.gif                {
50                    ici = codec;
51                }

52            }

53            EncoderParameters encoderParams = new EncoderParameters();
54            long[] qualityParam = new long[1];
55            if (Quality < 0 || Quality > 100)
56ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
65ExpandedSubBlockStart.gifContractedSubBlock.gif            {
66                img.Save(FileName, ici, encoderParams);
67            }

68            else
69ExpandedSubBlockStart.gifContractedSubBlock.gif            {
70                img.Save(FileName);
71            }

72
73            g.Dispose();
74            img.Dispose();
75            Sign.Dispose();
76            imageAttributes.Dispose();
77
78
79        }

 

 

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

ContractedBlock.gif ExpandedBlockStart.gif Code
 1public static void BuildDatePic(DateTime dt)
 2ExpandedBlockStart.gifContractedBlock.gif        {
 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")))
10ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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, 1059999);
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, 3913996);
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, 7289999);
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, 18319999);
34
35                if (File.Exists(filename))
36ExpandedSubBlockStart.gifContractedSubBlock.gif                {
37                    File.Delete(filename);
38                }

39                if (File.Exists(filename2))
40ExpandedSubBlockStart.gifContractedSubBlock.gif                {
41                    File.Delete(filename2);
42                }

43                if (File.Exists(filename3))
44ExpandedSubBlockStart.gifContractedSubBlock.gif                {
45                    File.Delete(filename3);
46                }

47                if (File.Exists(filename4))
48ExpandedSubBlockStart.gifContractedSubBlock.gif                {
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:日期可以不用图片,直接用文字生成在原图上,下次改进。这方面知识还比较缺乏,希望有人能指点一二,或者提供个实例看看。

转载于:https://www.cnblogs.com/Miler/archive/2009/06/03/1495529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值