由于需要图片上传的功能,所以花了一些时间网上找相关资料终于搞定,效果图如下:

下面的是解决方案截图和上传的图片截图:


具体实现代码如下:
1.界面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPic.aspx.cs" Inherits="Pic_Try.UploadPic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.easck.com/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.easck.com/1999/xhtml">
<head runat="server">
<title>图片上传和显示</title>
<style type="text/css">
.pic_text{ color:Red;}
.pic_label { color:Gray; margin-top:5px; margin-bottom:5px;}
.pic_image { margin:5px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="pic_image"><asp:Image ID="pic" runat="server" /></div>
<div><asp:FileUpload ID="pic_upload" runat="server" /><asp:Label ID="lbl_pic" runat="server" class="pic_text"></asp:Label></div>
<div class="pic_label">上传图片格式为.jpg, .gif, .bmp,.png,图片大小不得超过8M</div>
<div><asp:Button ID="btn_upload" runat="server" Text="上传" onclick="btn_upload_Click"/></div>
</form>
</body>
</html>
2.后台代码UploadPic.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;
using System.Web.Security;
namespace Pic_Try
{
public partial class UploadPic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_upload_Click(object sender, EventArgs e)
{
Boolean fileOk = false;
if (pic_upload.HasFile)//验证是否包含文件
{
//取得文件的扩展名,并转换成小写
string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();
//验证上传文件是否图片格式
fileOk = IsImage(fileExtension);
if (fileOk)
{
//对上传文件的大小进行检测,限定文件最大不超过8M
if (pic_upload.PostedFile.ContentLength < 8192000)
{
string filepath = "/images/";
if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(Server.MapPath(filepath));
}
string virpath = filepath + CreatePass










