积极答复者
动态生成Image,不显示图片

问题
-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplicationTest.WebForm2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:Panel ID="pnlShow" runat="server"> </asp:Panel> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplicationTest { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Image img = new Image(); img.ImageUrl = @"E:\Band.jpg"; pnlShow.Controls.Add(img); } } }
明明E:\Band.jpg有这个文件,为什么浏览器里不显示这个图片
浏览器的源文件是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form method="post" action="WebForm2.aspx" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTA3NjE3NzI4ZGS0Ysnrbgw+H771xNEWjoiJwtUvncOgQ1vxiZxN7jNn7w==" /> </div> <div id="pnlShow"> <img src="E:\Band.jpg" /> </div> </form> </body> </html>
答案
-
我建议你把图片文件全部拷贝到项目中,然后进行相对路径引用,便于管理;src后面的路径都是相对服务器根目录而言的,不能这样直接去指定路径。
- 已标记为答案 Song TianModerator 2012年9月13日 14:51
全部回复
-
我建议你把图片文件全部拷贝到项目中,然后进行相对路径引用,便于管理;src后面的路径都是相对服务器根目录而言的,不能这样直接去指定路径。
- 已标记为答案 Song TianModerator 2012年9月13日 14:51
-
你这不叫动态生成Image,动态生成图片一般在.net上都是用gdi+的,你引用路径换成相对虚拟目录路径的就可以了,不要用绝对文件路径
- 已编辑 gsralexModerator 2012年9月8日 13:47