我将图像存储在数据库中的varbinary(max)字段中。
我正在尝试在我的表单上显示一个图像。我尝试了许多不同的方法,但都没有表现出来。
这是我的代码:
//I am adding an image in my form
Ext.Net.Image image = new Ext.Net.Image();
image.ID = "imgShow";
FormLayout1.Items.Add(image);
byte[] buffer = q.TckLogo;
MemoryStream memStream = new MemoryStream();
memStream.Write(buffer, 0, buffer.Length);
// it is giving error because of FromStream
image.items.Add(Image.FromStream(memStream)); 如何在表单中显示图像?
我使用的是Ext.Net (酷派)。
发布于 2011-02-20 17:43:04
在web应用程序中,图像控件有一个ImageUrl属性,该属性指向将返回图像的某个服务器端脚本。我不熟悉Ext.Net.Image,但我认为您需要使用http处理程序来提供图像:
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var id = context.Request["id"];
byte[] imageData = FetchImageFromDb(id);
context.Response.ContentType = "image/png";
context.Response.OutputStream.Write(imageData, 0, imageData.Length);
}
public bool IsReusable
{
get { return true; }
}
}然后让您的图像控件指向这个通用处理程序:
Ext.Net.Image image = new Ext.Net.Image();
image.ID = "imgShow";
// pass some id to the generic handler which will
// allow it to fetch the image from the database
// and stream it to the response
image.ImageUrl = "~/ImageHandler.ashx?id=123";
FormLayout1.Items.Add(image);
image.items.Add(image);发布于 2011-02-20 17:43:52
我不清楚Ext.Net是什么,也不清楚你为什么要使用它,但FromStream的用法表明你对System.Drawing.Image.FromStream感到困惑;然而,当你时没有意义的事件不应该使用web应用程序中的 System.Drawing。如果您只是尝试将图像返回给客户端,则在许多情况下根本不需要处理它-只需将BLOB传递给客户端即可。
无关,但正如Jon所指出的,您也在尝试从MemoryStream的末尾进行读取。
https://stackoverflow.com/questions/5056184
复制相似问题