How to show image from database in asp: image with linq?

This is my table in the database:

enter image description here

And I read the database as:

DataClassesDataContext db = new DataClassesDataContext(); usertable thisuser = db.usertables.First(p => p.username == User.Identity.Name); 

So thisuser.picture is an image descriptor. But how can I show it in asp: image management on my page?

Edit I save the image with this code:

 DataClassesDataContext db = new DataClassesDataContext(); usertable thisuser = db.usertables.First(p => p.username == User.Identity.Name); byte[] filebyte = FileUpload1.FileBytes; System.Data.Linq.Binary fileBinary = new System.Data.Linq.Binary(filebyte); thisuser.picture = fileBinary; db.SubmitChanges(); 

something is wrong?

+4
source share
6 answers

The ASP.NET Image control explicitly represents the <img> in HTML. As a result, you can only get the image in the HTML document by specifying the URL of the content of the image that you want to insert on the page.

 <img src="images/picture.png" /> 

This means that you need a mechanism to request an HTTP request with an image resource and return a response containing binary image data.

With the ASP.NET Web API, this becomes a trivial operation to implement:

 public HttpResponseMessage GetImage(string username) { DataClassesDataContext db = new DataClassesDataContext(); usertable thisuser = db.usertables.FirstOrDefault( p => p.username == username); if (thisuser == null) { return new HttpResponseMessage(HttpStatusCode.NotFound)); } // Create a stream to return to the user. var stream = new MemoryStream(thisuser.picture.ToArray()); // Compose a response containing the image and return to the user. var result = new HttpResponseMessage(); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); return result; } 

If you cannot use the web API, you will have to implement an HTTP handler to do the same job.

On the ASP.NET page, you will need to set the ImageUrl property to the address configured for your controller / handler, including the username as part of the URL.

+4
source
 <asp:Image ImageUrl='<%# GetImage(Eval("IMG_DATA")) %>' /> 

Above, we tell the ASPX engine that we want to accept the value of the [IMG_DATA] column and pass it to the GetImage method on the page, which should return the correct image. So, give the opportunity to implement the method inside our page class:

 public string GetImage(object img) { return "data:image/jpg;base64," + Convert.ToBase64String((byte[])img); } 
+4
source

You will need to create a common handler - call ImageHandler.ashx and it will be at the root of your web application.

 public class ImageHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { // here is the tricky part - you don't store image type anywhere (you should) // assuming jpeg context.Response.ContentType = "image/jpeg"; DataClassesDataContext db = new DataClassesDataContext(); if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null) { var thisuser = db.usertables.First(p => p.username == HttpContext.Current.User.Identity.Name); if (thisuser != null) { byte[] buffer = thisuser.picture.ToArray(); context.Response.OutputStream.Write(buffer, 0, buffer.Length); } } } public bool IsReusable { get { return false; } } } 

Then on the page add:

 <asp:Image runat="server" ImageUrl="~/ImageHandler.ashx" /> 

A few notes:

you should consider image caching

you should switch from sql-type image to varbinary ( http://msdn.microsoft.com/en-us/library/ms187993.aspx )

you must store the mimetype of the image somewhere (if it is jpeg, png, bmp) - preferably in the same table, and the server correctly sets the mimetype in the handler

  • -
+4
source

If thisuser.picture has the correct path to your image, you can try something like this:

 Image im = new Image(); im.ID = "myImg"; im.ImageUrl = thisuser.picture.toString(); this.Controls.Add(im); 

I assume you are using web forms (I do not think this matters).

In addition, this may require a page refresh. 'this' refers to the Page object (in my case).

0
source

You cannot display it directly using the ASP.NET Image control (or at least not what I know right away). Quickly from the head:

Solution 1: You need the HttpHandler for your images. In your asp: image tag, you need ImageUrl to be selected by this particular HttpHandler. There you can write the contents of the image from the database as bytes using the correct MIME type and headers.

Solution 2: Take a look at embedding data in URLs, maybe you can write your image as follows and add it to asp: image ImageUrl: https://en.wikipedia.org/wiki/Data_URI_scheme

0
source

create an ashx page. Use something like this on this

 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; Stream strm = new MemoryStream(GetImage(ID)); long length = strm.Length; byte[] buffer = new byte[length]; int byteSeq = strm.Read(buffer, 0, 2048); while (byteSeq > 0) { context.Response.OutputStream.Write(buffer, 0, byteSeq); byteSeq = strm.Read(buffer, 0, 2048); } } 
  • ON Get image method

     public static byte[] GetImage(string ImageId) { byte[] img = null; DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "USP_SELECT_GLOBAL_METHOD"; cmd.Parameters.AddWithValue("@EVENT", 5); cmd.Parameters.AddWithValue("@CODE", ImageId); cmd.Connection = DL_CCommon.Connection(); SqlDataReader dr = null; dr = cmd.ExecuteReader(); if (dr.Read()) { img = (byte[])dr[0]; } dr.Close(); return img; } 

    this method returns the image as ur ID.

  • on your aspx page.

     Image1.ImageUrl = "somthing.ashx?ID="+userImageID; 

I hope this works. as i found this worked in my own. thank you

0
source

Source: https://habr.com/ru/post/1486959/


All Articles