C# 如何将图片读到内存在显示

luojingjing 2009-03-18 10:01:39
string str = "C:/Inetpub/wwwroot/pagerror.gif";
FileStream fs = new FileStream(str , FileMode.Open, FileAccess.Read);
byte[] image = new byte[fs.Length];
fs.Read(image, 0, Convert.ToInt32(fs.Length));
fs.Close();
fs.Dispose();
Response.ClearContent();
Response.BinaryWrite(image);
Response.End();
我用上面的方法是可以正确地把硬盘上的图片读到内存中并显示出来,但是如果图片是网站上的图片就不知道怎么显示了,比如
string str = "http://www.scottwilson.com.hk/images/1.jpg";,程序运行后会出错。

其中str是从数据库读出来的,可能是硬盘上的图片,也可能是网站上的图片

请高手指教
...全文
323 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuyue_xm 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
System.Net.WebClient _Client = new System.Net.WebClient();
byte[] _ImageBytes = _Client.DownloadData(p_ImagePath);

把_ImageBytes输出了就好了

Response.ClearContent();
Response.BinaryWrite(_ImageBy……
[/Quote]
你的方法很棒!!想求教一下,如何把读入的web图片作为本地的图片再输出?谢谢~~
chuxue1342 2009-03-18
  • 打赏
  • 举报
回复
如果网站上的图片,直接用网址不就能显示了吗?
luojingjing 2009-03-18
  • 打赏
  • 举报
回复
因为这个字段是用户输入的,我们不能控件他输入的是什么内容呀
嘴哥臭鼬 2009-03-18
  • 打赏
  • 举报
回复
那就加上http://啊
luojingjing 2009-03-18
  • 打赏
  • 举报
回复
zgke,你好,我用了你的方法后,用p_ImagePath=“http://www.scottwilson.com.hk/images/1.jpg”能正常显示,如果是用www.scottwilson.com.hk/images/1.jpg就出错了,不知道能不能解决呢?

[Quote=引用 2 楼 zgke 的回复:]
System.Net.WebClient _Client = new System.Net.WebClient();
byte[] _ImageBytes = _Client.DownloadData(p_ImagePath);

把_ImageBytes输出了就好了

Response.ClearContent();
Response.BinaryWrite(_ImageBytes);
Response.End();
[/Quote]
zgke 2009-03-18
  • 打赏
  • 举报
回复
System.Net.WebClient _Client = new System.Net.WebClient();
byte[] _ImageBytes = _Client.DownloadData(p_ImagePath);

把_ImageBytes输出了就好了

Response.ClearContent();
Response.BinaryWrite(_ImageBytes);
Response.End();
萤火架构 2009-03-18
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
string sRmFile = "http://zi.csdn.net/1000huawei.gif";
string sSavePath = "c:\1000huawei.gif";
System.Net.WebClient wc = new System.Net.WebClient();
try
{
wc.DownloadFile(sRmFile, sSavePath);
Response.Write("远程文件已经下载完毕!");
}
catch (System.Exception ex)
{
Response.Write("error:" + ex.Message.ToString());
}
}

文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/2007112/81969.html

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧