现在的位置: 首页 > 综合 > 正文

ASP.NET图象处理详解(2)

2011年11月08日 ⁄ 综合 ⁄ 共 4506字 ⁄ 字号 评论关闭
  二、读取和改变图象文件大小

   读取图片?直接使用HTML不就可以了?当然可以,我们这里只是提供一种选择和方法来实现这一功能,具体这一功能的使用,我们可能需要在实践中更多的学习。先来看程序源代码:
 

 1using System;
 2using System.Drawing;
 3using System.Drawing.Imaging;
 4using System.IO;
 5
 6private void sendFile()
 7        {
 8            System.Drawing.Image g = System.Drawing.Image.FromFile(Server.MapPath(Request["src"]));
 9            System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;
10            Bitmap imgOutput = new Bitmap(g,Convert.ToInt32(Request["width"]),Convert.ToInt32(Request["height"]));
11            if(thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
12            {
13                Response.ContentType="image/gif";
14            }

15            else
16            {
17                Response.ContentType="image/jpeg";
18            }

19            imgOutput.Save(Response.OutputStream,thisFormat);
20            g.Dispose();
21            imgOutput.Dispose();
22        }

23
24        private void sendError()
25        {
26            Bitmap imgOutput = new Bitmap(120,120,PixelFormat.Format24bppRgb);
27            Graphics g = Graphics.FromImage(imgOutput);
28            g.Clear(Color.Yellow);
29            g.DrawString("错误!",new Font("黑体",14,FontStyle.Bold),SystemBrushes.WindowText,new PointF(2,2));
30
31            Response.ContentType="image/gif";
32            imgOutput.Save(Response.OutputStream,ImageFormat.Gif);
33            g.Dispose();
34            imgOutput.Dispose();
35        }

36
37
38                        Response.Clear();
39            if (Request["src"]==""|| Request["height"]==""||Request["width"]=="")
40            {
41                sendError();
42            }

43            else
44            {
45                if(File.Exists(Server.MapPath(Request["src"])))
46                {
47                    sendFile();
48                }

49                else
50                {
51                    sendError();
52                }

53            }

54            Response.End();

在以上的程序中,我们看到两个函数,一个是SendFile,这一函数主要功能为显示服务器上的图片,该图片的大小通过Width和Height设置,同时,程序会自动检测图片类型;另外一个是SendError,这一函数的主要功能为服务器上的图片文件不存在时,显示错误信息,这里很有趣,错误信息也是通过图片给出的(如图):
     不出错时显示图片

三、画图特效

   如果只是将图片显示在网页上,这样未免显得简单。现在,我们来进一步感受ASP.NET的强大功能。我们将学习图象处理中常用的图象反转、图象切割、图象拉伸等技巧。
 先来看看程序效果:

仔细看,我们可以找到各种图象处理效果。现在,我们来看看程序代码:

 1using System;
 2using System.Drawing;
 3using System.Drawing.Imaging;
 4using System.Drawing.Drawing2D;
 5using System.IO;
 6
 7string strFilename;
 8            System.Drawing.Image i;
 9            strFilename = Server.MapPath("./1.jpg");
10
11            i=System.Drawing.Image.FromFile(strFilename);
12
13            Bitmap b = new Bitmap(i.Width,i.Height,PixelFormat.Format24bppRgb);
14            Graphics g = Graphics.FromImage(b);
15            g.Clear(Color.Blue);
16
17            //旋转图片
18            i.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX);
19            g.DrawImage(i,new Point(0,0));
20            i.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipY);
21
22            g.RotateTransform(10);
23            g.DrawImage(i,new Point(0,0));
24            g.RotateTransform(10);
25            g.DrawImage(i,new Point(20,20));
26            g.RotateTransform(10);
27            g.DrawImage(i,new Point(40,40));
28            g.RotateTransform(10);
29            g.DrawImage(i,new Point(40,40));
30            g.RotateTransform(-40);
31            g.RotateTransform(90);
32            g.DrawImage(i,new Rectangle(100,-400,100,50),new Rectangle(20,20,i.Width-20,i.Height-20),GraphicsUnit.Pixel);
33            g.RotateTransform(-90);
34
35            //拉伸图片
36            g.DrawImage(i,new Rectangle(10,10,50,50),new Rectangle(20,20,i.Width-20,i.Height-20),GraphicsUnit.Pixel);
37            g.DrawImage(i,new Rectangle(50,10,90,50),new Rectangle(20,20,i.Width-20,i.Height-20),GraphicsUnit.Pixel);
38            g.DrawImage(i,new Rectangle(110,10,150,50),new Rectangle(20,20,i.Width-20,i.Height-20),GraphicsUnit.Pixel);
39
40
41            //切割图片
42            g.DrawImage(i,50,100,new Rectangle(180,80,60,110),GraphicsUnit.Pixel);
43            g.DrawImage(i,230,100,new Rectangle(180,80,60,110),GraphicsUnit.Pixel);
44
45            //旋转图片
46            i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
47            g.DrawImage(i,230,100,new Rectangle(180,110,60,110),GraphicsUnit.Pixel);
48
49            Response.ContentType="image/jpeg";
50            b.Save(Response.OutputStream,ImageFormat.Jpeg);
51            b.Dispose();
52

在以上的程序中,我们看到实现图象处理的各种技巧,仔细观察,我们可以知道旋转图片其实是用了一个RotateFlip方法;而切割和拉伸图片,完全是通过设置DrawImage的不同参数来实现。
 
   四、总结

   ASP.NET的图象处理可以实现的功能很多,我们在这里其实只是简单的介绍,更多功能的应用,需要我们在实践中摸索、总结。

抱歉!评论已关闭.