ASP
  项目合作 | 广告服务 | 联系我们 | 友情链接 | 链接代码 | 短信服务 | 给我留言 | 返回首页                    
设为首页
加入收藏
ASP技术 | ASP.NET技术 | JSP技术 | Servlet技术 | PHP技术 | JS技术 | C#技术 | Java技术 | B/S应用 | B/S论坛 | 下载区
 
 自定义DataGrid分页设置
 ASP.NET中使用Caching
 一个文件上传的类
 ASP.NET中在线用户统计
 网站间共享数据的WebService
 ASP.NET里的事务处理
 VS.NET下web项目源代码管理
 用TreeView实现树菜单
 在DataGrid中创建一个弹出式窗.
 有关DataGrid显示数据的问题
 让Calendar在页面调用时才显示.
 在web.config中建立数据库连接.
 实时天气及24小时天气预报
 数据库中图片存储及读取
 如何在窗体和窗体之间传送数据.
 利用ASP.NET来访问Excel文档
 使用嵌套的Repeater控件
 用asp.net画饼图
 实现DataList控件的分页
 用ASP.NET加密口令
 使用RangeValidator
 C#做的ASP.NET登錄篇
 ASP.NET图象处理详解
 在网页中动态的生成一个图片
 检测含有中文字符串的实际长度.
 用ASP.NET和XML做的新闻系统
 如何在asp.net中操作文件
 ASP.NET验证控件祥解
 ASP.NET中的事务处理和异常处理.
 ASP.NET上传文件的实例
 在ASP.NET中访问SQL Server
 ASP.NET安全身份验证的实现
 ASP.NET中密码保护
 在ASP.NET中使用.NET组件
 实现Web文件的上传
 asp.net实现pop功能
 ASP.NET创建文件并写入内容
 在ASP.NET中动态生成图形
 ASP.NET中文显示之两种解决方法.
 ASP.NET验证控件详解
 在ASP.NET中使用.NET组件
 资料验证的asp.net程序
 一个完整的案例
 在ASP+的Form中检查填写是否正.
 增加修改删除一个数据
 个性化的分页实现
 用ASP.NET识别浏览器
 DataGrid学习七
 DataGrid学习六
 DataGrid学习五
 DataGrid学习四
 DataGrid学习三
 DataGrid学习二
 DataGrid学习一
 DataTable控件的使用
 RegularExpressionValidator
 ViewState初探
 ASP.Net的Session
 ASP.Net的Application
 AdRotator控件的使用
 ASP.Net的Cookie实现
 XML、DataSet、DataGrid结合二.
 XML、DataSet、DataGrid结合一.
 ASP.NET中的Web Controls
 ASP.NET程序数组功能调用
 ASP.NET与ASP的不同
 ASP.net发送Email
 ASP.NET、JSP及PHP之间的抉
 

ASP.NET图象处理详解

源作者:喻凯                   人气:4648
  在使用ASP的时候,我们时常要借助第三方控件来实现一些图象功能。而现在,ASP.NET的推出,我们已经没有必要再使用第三方控件来实现,因为ASP.NET 已经具有强大的功能来实现一些图象处理。现在,我们就来看看怎样使用ASP.NET的这一强大功能。
 
一、System.Drawing的使用
  以下的举例将演示在内存中生成一张图片,然后,将这张图片通过网页显示出来。需要了解的是,我们这里输出的不是HTML效果,而是实实在在的图片(图象),我们可以使用“另存为…”将输出图象保存起来。
我们来看源代码:
<%@ page language="vb" contenttype="image/jpeg" %> 
<%@ import namespace="system.drawing" %> 
<%@ import namespace="system.drawing.imaging" %> 
<%@ import namespace="system.drawing.drawing2d" %> 
 
<% 
"清空Response 
response.clear  
"建立一个120*30大小,24bit的BMP图象;
dim imgOutput as New bitmap(120, 30, pixelformat.format24bpprgb)  
"根据以上BMP建立一个新图象;
dim g as graphics = graphics.fromimage(imgOutput) 
g.clear(color.Green)
g.smoothingMode = smoothingMode.antiAlias 
g.drawString("看见了吗?", New font("黑体",16,fontstyle.bold),new SolidBrush(Color.White),New pointF(2,4))  
g.FillRectangle(New linearGradientBrush(New point(0,0), New point(120,30), color.fromArgb(0,0,0,0),color.fromArgb(255,255,255,255)),0,0,120,30) 
imgOutput.save(response.outputstream, imageformat.jpeg) 
g.dispose() 
imgOutput.dispose() 
response.end 
%> 
在以上代码中,我们看到和数据库程序不同,这里专门引入了图象处理的名字空间system.drawing等。程序首先清空了Response,确保没有输出;然后,程序建立了一个120乘30大的BMP图象,再在这个基础上建立一个新图象,建立图象以后,我们首先“画”出了字符串“看见了吗”,该字符串为16大粗黑体,颜色为白色,位置为(2,4);最后,我们实现渐变效果。
以上举例很简单,但是如果和数据库结合,我们可以实现很多使用ASP可能不敢想的效果。
 
二、读取和改变图象文件大小
读取图片?直接使用HTML不就可以了?当然可以,我们这里只是提供一种选择和方法来实现这一功能,具体这一功能的使用,我们可能需要在实践中更多的学习。先来看程序源代码:
<% " import all relevant namespaces %> 
<%@ import namespace="System" %> 
<%@ import namespace="System.Drawing" %> 
<%@ import namespace="System.Drawing.Imaging" %> 
<%@ import namespace="System.IO" %>  
<script runat="server"> 
Sub sendFile() 
dim g as System.Drawing.Image = System.Drawing.Image.FromFile(server.mappath(request("src"))) 
dim thisFormat=g.rawformat 
dim imgOutput as New Bitmap(g, cint(request("width")), cint(request("height"))) 
if thisformat.equals(system.drawing.imaging.imageformat.Gif) then 
response.contenttype="image/gif" 
else 
response.contenttype="image/jpeg" 
end if 
imgOutput.save(response.outputstream, thisformat)
g.dispose() 
imgOutput.dispose() 
end sub 
 
Sub sendError() 
dim imgOutput as New bitmap(120, 120, pixelformat.format24bpprgb) 
dim g as graphics = graphics.fromimage(imgOutput) 
g.clear(color.yellow)
g.drawString("错误!", New font("黑体",14,fontstyle.bold),systembrushes.windowtext, New pointF(2,2)) 
response.contenttype="image/gif" 
imgOutput.save(response.outputstream, imageformat.gif) 
g.dispose() 
imgOutput.dispose() 
end sub 
</script> 
 
<% 
response.clear 
if request("src")="" or request("height")="" or request("width")="" then 
call sendError() 
else 
if file.exists(server.mappath(request("src"))) then 
call sendFile() 
else 
call sendError() 
end if 
end if 
response.end 
%> 
在以上的程序中,我们看到两个函数,一个是SendFile,这一函数主要功能为显示服务器上的图片,该图片的大小通过Width和Height设置,同时,程序会自动检测图片类型;另外一个是SendError,这一函数的主要功能为服务器上的图片文件不存在时,显示错误信息,这里很有趣,错误信息也是通过图片给出的.
以上的程序显示图片并且改变图片大小,现在,我们将这个程序进一步,显示图片并且保持图片的长宽比例,这样,和实际应用可能比较接近,特别是需要制作电子相册或者是图片网站的时候比较实用。我们先来看主要函数:
Function NewthumbSize(currentwidth, currentheight) 
dim tempMultiplier as Double 
if currentheight > currentwidth then
tempMultiplier = 200 / currentheight 
Else 
tempMultiplier = 200 / currentwidth 
end if 
dim NewSize as New Size(CInt(currentwidth * tempMultiplier), CInt(currentheight * tempMultiplier)) 
return NewSize 
End Function 
以上程序是增加的一个函数NewthumbSize,该函数专门处理改变一会的图片大小,这个图片的长宽和原图片的长宽保持相同比例。其他部分请参考上文程序代码。
 
三、画图特效
如果只是将图片显示在网页上,这样未免显得简单。现在,我们来进一步感受ASP.NET的强大功能。我们将学习图象处理中常用的图象反转、图象切割、图象拉伸等技巧。
现在,我们来看看程序代码:
<%@ Page Language="vb" Debug="True" %> 
<%@ import namespace="system.drawing" %> 
<%@ import namespace="system.drawing.imaging" %> 
<%@ import namespace="system.drawing.drawing2d" %> 
<% 
dim strFilename as string 
dim i as System.Drawing.Image 
strFilename = server.mappath("./chris-fsck.jpg") 
i = System.Drawing.Image.FromFile(strFilename)  
dim b as New system.drawing.bitmap(i.width, i.height, pixelformat.format24bpprgb) 
dim g as graphics = graphics.fromimage(b)   
g.clear(color.blue)   
"旋转图片
i.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX)  g.drawimage(i,New point(0,0)) 
i.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipY)   
g.RotateTransform(10) 
g.drawimage(i,New point(0,0)) 
g.RotateTransform(10) 
g.drawimage(i,New point(20,20)) 
g.RotateTransform(10) 
g.drawimage(i,New point(40,40)) 
g.RotateTransform(10) 
g.drawimage(i,New point(40,40)) 
g.RotateTransform(-40) 
g.RotateTransform(90) 
g.drawimage(i,New rectangle(100,-400,100,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel) 
g.RotateTransform(-90)   
" 拉伸图片
g.drawimage(i,New rectangle(10,10,50,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel) 
g.drawimage(i,New rectangle(50,10,90,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel) 
g.drawimage(i,New rectangle(110,10,150,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)   
"切割图片
g.drawimage(i,50,100,New rectangle(180,80,60,110),GraphicsUnit.Pixel) 
g.drawimage(i,140,100,New rectangle(180,80,60,110),GraphicsUnit.Pixel)   
"旋转图片
i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX) 
g.drawimage(i,230,100,New rectangle(180,110,60,110),GraphicsUnit.Pixel)   
response.contenttype="image/jpeg"   
b.save(response.outputstream, imageformat.jpeg)   
b.dispose()   
%>
在以上的程序中,我们看到实现图象处理的各种技巧,仔细观察,我们可以知道旋转图片其实是用了一个RotateFlip方法;而切割和拉伸图片,完全是通过设置DrawImage的不同参数来实现。
 
四、总结
ASP.NET的图象处理可以实现的功能很多,我们在这里其实只是简单的介绍,更多功能的应用,需要我们在实践中摸索、总结。

如有疑问,请赐电邮:webmaster@chinabs.net  OICQ:28194826
技术开发:深圳市百越软件工作室
中国BS网版权所有     Copyright chinabs.net