如何显示System.Drawing.Image图像

OICQshenshi 2008-09-26 11:08:10
System.Drawing.Image img = System.Drawing.Image.FromStream(str);
Image1.ImageUrl = (string)img;

显示img对象的控件可以是WEB Image也可以是HTML的Image,求最简单的代码。
...全文
1168 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlzdq521 2009-02-12
  • 打赏
  • 举报
回复
学习学习!!支持
OICQshenshi 2008-09-27
  • 打赏
  • 举报
回复
上回复红体部分是
(本地资源文件)TestCulture.aspx.resx (http_imgloadCA16SSXN.jpg图片嵌入在此资源文件中即属性已经改为“嵌入的资源”)
OICQshenshi 2008-09-27
  • 打赏
  • 举报
回复
==大家方法都很好,但现在出现新问题。

断点时str值为null。

        // 创建当前执行的代码程序集的对象 
Assembly asm = Assembly.GetExecutingAssembly();
string ResourceName = "http_imgloadCA16SSXN.jpg";

// 从此程序集清单中加载指定的清单资源


"http_imgloadCA16SSXN.jpg"图片是嵌入在(站点A)根目录的TestCulture.aspx.resx文件中的。
目录结构如下:

(站点)A
(文件夹)Culture
(本地资源文件夹)App_LocalResources
(本地资源文件)TestCulture.aspx.fr-FR.resx
(本地资源文件)TestCulture.aspx.fr-FR.resx (http_imgloadCA16SSXN.jpg图片嵌入在此资源文件中即属性已经改为“嵌入的资源”)
(图片)http_imgloadCA16SSXN.jpg

(ASPX文件)ImageStream.aspx


启动ImageStream.aspx后报错
        //创建当前执行的代码程序集的对象 
Assembly asm = Assembly.GetExecutingAssembly();
string ResourceName = "http_imgloadCA16SSXN.jpg";

//从此程序集清单中加载指定的清单资源
Stream str = asm.GetManifestResourceStream(ResourceName);

asm和str都是null(空)的,我想问题是出在ResourceName中,问题可能和此帖一样
https://forum.csdn.net/PointForum/Old/TopicRedirect.ashx?id=5378404

烦大家帮我看看到底哪错了?先非常感谢了
lutuyao 2008-09-27
  • 打赏
  • 举报
回复
System.Drawing.Image img = System.Drawing.Image.FromStream(str);
img.Save("abc.jpg")//保存图片,比如放到服务器根目录,文件名为abc.jpg
Image1.ImageUrl = "abc.jpg"
jinhesoft 2008-09-27
  • 打赏
  • 举报
回复
行 28: Response.AddHeader("Content-Type", "Image/JPEG");
行 29: byte[] bs = new byte[str.Length];
行 30: str.Read(bs, IntPtr.Zero, str.Length);
行 31: Response.BinaryWrite(bs);

该为


Response.OutputStream.Write(str, 0, str.Length);

OICQshenshi 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 calmer18 的回复:]
str.Read(bs, IntPtr.Zero, str.Length);

这句不要用IntPtr.Zero,直接用str.Read(bs, 0, str.Length);


[/Quote]
还是报错。。
编译器错误信息: CS1502: 与“System.IO.Stream.Read(byte[], int, int)”最匹配的重载方法具有一些无效参数
OICQshenshi 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chen_lichao 的回复:]
C# code
Stream stream;
//从资源文件取出Stream给stream
FileStream f = stream as FileStream;

Response.WriteFile(f.Handle,0,f.Length);



试下行不行
[/Quote]
不行的。

//创建当前执行的代码程序集的对象
Assembly asm = Assembly.GetExecutingAssembly();
string ResourceName = "http_imgloadCA16SSXN.jpg";

//从此程序集清单中加载指定的清单资源
Stream str = asm.GetManifestResourceStream(ResourceName);
System.Drawing.Image img = System.Drawing.Image.FromStream(str);

caofan520 2008-09-26
  • 打赏
  • 举报
回复

Stream stream;
//从资源文件取出Stream给stream
FileStream f = stream as FileStream;

Response.WriteFile(f.Handle,0,f.Length);


试下行不行
caofan520 2008-09-26
  • 打赏
  • 举报
回复
要新建一张页面在输出流
然后控件指向这张页面
OICQshenshi 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chen_lichao 的回复:]
这种情况

新建一张页img.aspx
根据ID号取出数据库的图片然后输出
然后img.ImageUrl = "img.aspx?id=1"
[/Quote]

是资源文件。。。


问题简单点就是怎么在网页中显示这个img流?
caofan520 2008-09-26
  • 打赏
  • 举报
回复
这种情况

新建一张页img.aspx
根据ID号取出数据库的图片然后输出
然后img.ImageUrl = "img.aspx?id=1"
OICQshenshi 2008-09-26
  • 打赏
  • 举报
回复
能给个其他方法在网页上显示这个img图像也成啊
OICQshenshi 2008-09-26
  • 打赏
  • 举报
回复
str是IMAGE资源文件流
OICQshenshi 2008-09-26
  • 打赏
  • 举报
回复
str是IMAGE资源文件
caofan520 2008-09-26
  • 打赏
  • 举报
回复
str是Stream不是路径啊

这样搞不行吧
wuyi8808 2008-09-26
  • 打赏
  • 举报
回复
System.Drawing.Image img = System.Drawing.Image.FromStream(str);
Image1.ImageUrl = (string)img;

改成

Image1.ImageUrl = str;
dengchenlu 2008-09-26
  • 打赏
  • 举报
回复
Image1.ImageUrl = img;//直接写路径就可以了吧
calmer18 2008-09-26
  • 打赏
  • 举报
回复
str.Read(bs, IntPtr.Zero, str.Length);

这句不要用IntPtr.Zero,直接用str.Read(bs, 0, str.Length);

calmer18 2008-09-26
  • 打赏
  • 举报
回复
问题一:
byte[] bs = new byte[str.Length]; str中没有ContentLength这项。

---------------------------------

不好意思,是我写错了,应该是byte[] imgBytes=new byte[yourStream.Length];
wuyi8808 2008-09-26
  • 打赏
  • 举报
回复
MyImage.aspx.cs:

// 创建当前执行的代码程序集的对象
Assembly asm = Assembly.GetExecutingAssembly();
string ResourceName = "http_imgloadCA16SSXN.jpg";

// 从此程序集清单中加载指定的清单资源
Stream str = asm.GetManifestResourceStream(ResourceName);
FileStream f = str as FileStream;

// 输出该图像
Response.WriteFile(f.Handle, 0, f.Length);



index.aspx.cs:
        Image1.ImageUrl = "MyImage.aspx"; 
加载更多回复(2)

62,046

社区成员

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

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

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

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