如何在c#中将图像加载到图像控件?

时间:2013-12-18 10:54:51

标签: c# asp.net image

我正在尝试将图像加载到我从工具中拖动的图像控件。被命名为'Logo' 我给出的文件路径来自我的D:\ Photos \。

我编写的代码如下

 Logo.ImageUrl ="D:\Photos\image1.jpeg" ;

图像控制代码如下

    <asp:Image ID="Logo" runat="server" Height="99px" 
         Width="119px" />

图像出现在给定位置但未加载。它没有给出任何错误。

任何人都可以告诉我在运行时加载图像需要做哪些更改。?

4 个答案:

答案 0 :(得分:0)

问题:由于physical paths问题,您无法从网络应用访问Drives client machine的{​​{1}}。

解决方案:
1.在当前项目文件夹中创建security文件夹 2.将所有必需的图像复制到Photos文件夹中 3.从Photos文件夹路径访问图像 4.提供Photos tilde运算符以指定~文件夹 5.使用current project函数将给定的Server.MapPath()转换为relative path

试试这个:

absolute path

答案 1 :(得分:0)

要么尝试@Sudhakar Tillapudi说的话 做类似文件:// D://Photos//image1.jpeg

您的Web应用程序可以安装在世界任何地方的Web服务器上以及任何给定的系统上,因此您有2个选项: - 将图片网址设置为网址:)

实施例

  

http://somewherefarfarawy.com/image.jpg

  • 将图片网址设置为本地资源的路径 file:// D://temp//logo.jpg在opera中被翻译为file:// localhost / D://temp//logo.jpg

答案 2 :(得分:0)

看起来你的路径不正确,在将项目存储到自己的

后,尝试使用Server.MapPath()

请阅读有关ASP.NET Web Project Paths

的文档

答案 3 :(得分:0)

使用处理程序显示图像

创建处理程序遵循此链接 http://www.dotnetperls.com/ashx

将处理程序添加到图像

<asp:Image runat="server" Width="40px" Height="40px" ImageUrl='<%# "Handler.ashx %>'>
处理程序中的

传递图像屁股字节流

public void ProcessRequest(HttpContext context)
{


  //get the image from data base in here im using a web service
    System.Data.DataSet ds = new System.Data.DataSet();
    MMS_MasterWebService.MMS_MasterMaintenance obj = new MMS_MasterWebService.MMS_MasterMaintenance();
    obj.Url =  "http://192.168.48.10/SHOREVision_MMS_Service/MMS_MasterMaintenance.asmx";
    ds = obj.GetVehicleMasterByCode(para);
    context.Response.BinaryWrite((byte[])ds.Tables[0].Rows[0][21]);

}