asp.net的gdi+绘图
本来用asp.net gdi+绘制一个图片,生产的是jpg格式的,可是清晰度不够高。 但是在这里换成png的却报错 System.Drawing.Imaging.ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
找了一堆资料,终于找到了,下面是代码!
Code
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Drawing.Imaging;
14
15public partial class CreateImg : System.Web.UI.Page
16{
17 protected void Page_Load(object sender, EventArgs e)
18 {
19
20
21
22
23 }
24 private static ImageCodecInfo GetEncoderInfo(String mimeType)
25 {
26 ImageCodecInfo[] encoders;
27 encoders = ImageCodecInfo.GetImageEncoders();
28 for (int j = 0; j < encoders.Length; ++j)
29 {
30 if (encoders[j].MimeType == mimeType)
31 return encoders[j];
32 }
33 return null;
34 }
35 private Bitmap GetneralCodeImage()
36 {
37
38 //创建位图
39
40 Bitmap bmp = new Bitmap(150, 18);
41
42 Graphics g = Graphics.FromImage(bmp);
43 g.Clear(Color.FromArgb(247, 213, 225));
44 //描绘字符
45 g.DrawString("1366666666", new Font("Verdana", 11, FontStyle.Bold),
46 new SolidBrush(Color.FromArgb(255, 102, 0)),
47 0,
48 0
49 );
50 g.CompositingQuality = CompositingQuality.HighQuality;
51 g.SmoothingMode = SmoothingMode.HighQuality;
52 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
53 g.PixelOffsetMode = PixelOffsetMode.HighQuality;
54 g.Dispose();
55 return bmp;
56 }
57
58 protected void btnpng_Click(object sender, EventArgs e)
59 {
60 //生成png图片(清晰度高)
61 System.IO.MemoryStream ms = new System.IO.MemoryStream();
62 GetneralCodeImage().Save(ms, ImageFormat.Png);
63 ms.WriteTo(Response.OutputStream);
64 }
65 protected void btnjpeg_Click(object sender, EventArgs e)
66 {
67 //生成jpg图片(清晰度不够)
68 System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
69 ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L);
70 System.Drawing.Imaging.ImageCodecInfo ici = GetEncoderInfo("image/jpeg");//这里换成gif后文字显示可以,但背景不清楚
71 this.GetneralCodeImage().Save(Response.OutputStream, ici, ep);
72 }
73}
74
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Drawing.Imaging;
14
15public partial class CreateImg : System.Web.UI.Page
16{
17 protected void Page_Load(object sender, EventArgs e)
18 {
19
20
21
22
23 }
24 private static ImageCodecInfo GetEncoderInfo(String mimeType)
25 {
26 ImageCodecInfo[] encoders;
27 encoders = ImageCodecInfo.GetImageEncoders();
28 for (int j = 0; j < encoders.Length; ++j)
29 {
30 if (encoders[j].MimeType == mimeType)
31 return encoders[j];
32 }
33 return null;
34 }
35 private Bitmap GetneralCodeImage()
36 {
37
38 //创建位图
39
40 Bitmap bmp = new Bitmap(150, 18);
41
42 Graphics g = Graphics.FromImage(bmp);
43 g.Clear(Color.FromArgb(247, 213, 225));
44 //描绘字符
45 g.DrawString("1366666666", new Font("Verdana", 11, FontStyle.Bold),
46 new SolidBrush(Color.FromArgb(255, 102, 0)),
47 0,
48 0
49 );
50 g.CompositingQuality = CompositingQuality.HighQuality;
51 g.SmoothingMode = SmoothingMode.HighQuality;
52 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
53 g.PixelOffsetMode = PixelOffsetMode.HighQuality;
54 g.Dispose();
55 return bmp;
56 }
57
58 protected void btnpng_Click(object sender, EventArgs e)
59 {
60 //生成png图片(清晰度高)
61 System.IO.MemoryStream ms = new System.IO.MemoryStream();
62 GetneralCodeImage().Save(ms, ImageFormat.Png);
63 ms.WriteTo(Response.OutputStream);
64 }
65 protected void btnjpeg_Click(object sender, EventArgs e)
66 {
67 //生成jpg图片(清晰度不够)
68 System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
69 ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L);
70 System.Drawing.Imaging.ImageCodecInfo ici = GetEncoderInfo("image/jpeg");//这里换成gif后文字显示可以,但背景不清楚
71 this.GetneralCodeImage().Save(Response.OutputStream, ici, ep);
72 }
73}
74