如何在asp.net(c#)中显示存储在网络上的图像

如何在asp.net(c#)中显示存储在网络上的图像,c#,asp.net,C#,Asp.net,如何在asp.net中显示存储在网络上的图像(使用c#)。我希望img src为以下给定值: \\pgapp\folder\image.jpg,其中pgapp是网络上的共享驱动器 我正在使用这些代码,但它没有显示在网页上的图像 originalImage2.ImageUrl=@"\\pgapp\folder\image.jpg"; 或 originalImage2.Attributes[“src”]=ResolveUrl(@“\\pgapp\folder\image.jpg”) 您将遇到两个问

如何在asp.net中显示存储在网络上的图像(使用c#)。我希望img src为以下给定值:

\\pgapp\folder\image.jpg
,其中pgapp是网络上的共享驱动器

我正在使用这些代码,但它没有显示在网页上的图像

originalImage2.ImageUrl=@"\\pgapp\folder\image.jpg";

originalImage2.Attributes[“src”]=ResolveUrl(@“\\pgapp\folder\image.jpg”)


您将遇到两个问题:

  • 浏览器通常无法访问服务器共享(除非它位于同一网络上)。你需要以某种方式通过你的站点公开图像,使其像
  • “NTLM一跳”-如果开始通过服务器渲染图像,服务器将无法从远程服务器读取文件。你只需要确认你是否有问题,并阅读相关资料

要在用户web浏览器中显示存储在网络上的图像,请执行以下操作:

将图像转换为base64对应图像并显示:

// PhotoId is the network file path and name.
string photoId = "\\Photos\2015-May\390828d1-8f20-4fe9-9287-13d03894e9c0.jpg"

// Call to display the networked images.
lbl_images.Text += "<img src='" + this.PhotoBase64ImgSrc(photoId) + "' height='60px' width='60px' alt='photo' />";

// Supporting function that converts an image to base64.
protected string PhotoBase64ImgSrc(string fileNameandPath)
{
    byte[] byteArray = File.ReadAllBytes(fileNameandPath);
    string base64 = Convert.ToBase64String(byteArray);

    return string.Format("data:image/gif;base64,{0}", base64);
}
//PhotoId是网络文件路径和名称。
字符串photoId=“\\Photos\2015年5月\390828d1-8f20-4fe9-9287-13d03894e9c0.jpg”
//调用以显示网络图像。
lbl_images.Text+=“”;
//支持将映像转换为base64的函数。
受保护的字符串PhotoBase64ImgSrc(字符串文件名和路径)
{
byte[]byteArray=File.ReadAllBytes(文件名和路径);
string base64=Convert.tobase64字符串(byteArray);
返回string.Format(“数据:image/gif;base64,{0}”,base64);
}

如果要显示存储在网络路径中的图像,请更改IIS中的以下设置

  • 导航到IIS
  • 导航到“站点”文件夹中的网站
  • 选择应用程序网站并导航到右侧的“操作”菜单,然后单击“查看虚拟目录”。
  • 单击添加虚拟目录,并将别名指定为“映像”,将物理路径指定为“网络驱动器路径”。
  • 测试设置并保存它
  • 刷新应用程序池并测试图像是否正在渲染

  • 进行调试时,图像src指向何处?如何在WebForms中显示图像?这不起作用,因为出于安全原因,现代浏览器不允许访问网站缓存之外的文件。因此,如果它是
    C:\outsidecache\file.jpg
    ,它将无法工作。我尝试了
    \\sharedfolder\file.jpg
    file://sharedfolder/file.jpg
    它就是不起作用。@sojim2我删除了我的评论,因为它是错误的。。。我说的是windowsforms而不是webforms。。。我在webforms应用程序中使用它。。。您必须使用可以访问网络位置的帐户在IIS中运行应用程序池。。。然后,应用程序池可以读取图像文件,将其转换为base64并将其作为字符串发送到浏览器。。。。然后使用它,就像我发现了一个更简单的方法,虚拟目录!那你不介意你的照片公开吗?为什么不把它们放在网站本身的文件夹中呢?(如果只有一个网站可以访问图像)。
    // PhotoId is the network file path and name.
    string photoId = "\\Photos\2015-May\390828d1-8f20-4fe9-9287-13d03894e9c0.jpg"
    
    // Call to display the networked images.
    lbl_images.Text += "<img src='" + this.PhotoBase64ImgSrc(photoId) + "' height='60px' width='60px' alt='photo' />";
    
    // Supporting function that converts an image to base64.
    protected string PhotoBase64ImgSrc(string fileNameandPath)
    {
        byte[] byteArray = File.ReadAllBytes(fileNameandPath);
        string base64 = Convert.ToBase64String(byteArray);
    
        return string.Format("data:image/gif;base64,{0}", base64);
    }