在asp.net中利用image控件可以显示图片。但是直接设置image.ImageUrl = “D:XXX"是无法显示的,这是我们可以曲线实现这个功能,将image.ImageUrl设置成”XXX.aspx",再在“XXX.aspx”中显示本地图片即可
1
|
<span>前台代码如下(</span>SocialNetWorkViewPage.aspx):
|
1
2
3
4
5
6
7
8
9
10
|
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeBehind=
"SocialNetWorkViewPage.aspx.cs"
MasterPageFile =
"~/Site.Master"
Inherits=
"SNSHunter.ViewPages.SocialNetWorkViewPage"
%>
<asp:Content ID=
"HeaderContent"
runat=
"server"
ContentPlaceHolderID=
"HeadContent"
>
</asp:Content>
<asp:Content ID=
"BodyContent"
runat=
"server"
ContentPlaceHolderID=
"MainContent"
>
<div align =
"center"
>
<asp:PlaceHolder ID =
"GraphPlaceHolder"
runat =
"server"
>
//这个控件是用来放图片的
</asp:PlaceHolder>
</div>
</asp:Content>
|
1
|
后台代码(SocialNetWorkViewPage.aspx.cs):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
SNSHunter.ViewPages
{
public
partial
class
SocialNetWorkViewPage : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(Session[
"UserName"
] ==
null
)
{
Response.Write(
"<script>window.location.href='../Account/Login.aspx'</script>"
);
}
else
{
//新建IMG,并显示
Image image =
new
Image();
//image.ImageUrl = excelPath + @"\UserSocial.png";
image.ImageUrl =
"~/ViewPages/ImagePage.aspx"
;
//跳转到新界面
GraphPlaceHolder.Controls.Add(image);
}
}
}
}
|
1
|
新界面后台代码(ImagePage.aspx.cs"):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
SNSHunter.ViewPages
{
public
partial
class
ImagePage : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
Response.WriteFile(
"xxx."
);
}
}
}
|
如果路径(字符串)存储在服务器的数据库当中,可以用reader读取,然后存储在session当中,并进行调用。