I am storing images in database in a varbinary(max)
field.
I am try to show an image on my form. I've tried many different ways but it did not show it.
This is my Code:
//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, buff开发者_运维问答er.Length);
// it is giving error because of FromStream
image.items.Add(Image.FromStream(memStream));
How can I display my image in my form?
I am using Ext.Net (Coolite).
In a web application, image controls have an ImageUrl
property pointing to some server side script which will return the image. I am not familiar with Ext.Net.Image
but I suppose that you need to use a http handler to serve the image:
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; }
}
}
and then have your image control point to this generic handler:
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);
It isn't clear to me what Ext.Net is here, or why you are using it, but the FromStream usage suggests you are confusing is with System.Drawing.Image.FromStream
; however, event that is moot as you shouldn't use System.Drawing from a web app. If you are just trying to return an image to the client, you shouldn't have to process it at all in many cases - just throw the BLOB down the wire to the client.
Unrelated, but as Jon notes you're also trying to read from the end of a MemoryStream.
精彩评论