Can someone help me??
How to retrive image from MySQL(not MS_SQL) and show into my <asp:Image> control using c#
Lets said my table on have a field as following:
"SELECT pic FROM image"
Thanks in advance !!
Can someone help me??
How to retrive image from MySQL(not MS_SQL) and show into my <asp:Image> control using c#
Lets said my table on have a field as following:
"SELECT pic FROM image"
Thanks in advance !!
Short answer: don’t write blobs to a temp file on each request — serve the bytes directly from the database through a small HTTP endpoint (Generic Handler or MVC action) and point your <asp:Image> at that endpoint. That avoids file I/O, makes caching simple, and keeps the web server in control of headers.
A recommended flow (contrast to comments above by and ): store the binary (MEDIUMBLOB/LONGBLOB) and the MIME type (e.g. "image/jpeg") in MySQL. Create a parameterized query to pull the row by id, and write the bytes to the response with the correct Content-Type. Example Generic Handler (ImageHandler.ashx):
using System;
using System.Configuration;
using System.Web;
using MySql.Data.MySqlClient;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (!int.TryParse(context.Request.QueryString["id"], out int id))
{
context.Response.StatusCode = 400; return;
}
string cs = ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;
using (var cn = new MySqlConnection(cs))
using (var cmd = new MySqlCommand("SELECT pic, mime FROM image WHERE id=@id", cn))
{
cmd.Parameters.AddWithValue("@id", id);
cn.Open();
using (var rdr = cmd.ExecuteReader())
{
if (!rdr.Read() || rdr.IsDBNull(0)) { context.Response.StatusCode = 404; return; }
byte[] bytes = (byte[])rdr["pic"];
string mime = rdr["mime"] as string ?? "image/jpeg";
context.Response.ContentType = mime;
context.Response.OutputStream.Write(bytes, 0, bytes.Length);
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(7));
}
}
}
public bool IsReusable { get { return false; } }
} Use the handler URL for your Image control:
<asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# "ImageHandler.ashx?id=" + Eval("id") %>' /> Or set it server-side: imgPhoto.ImageUrl = "ImageHandler.ashx?id=" + id;
Troubleshooting and cautions: always use parameterized queries to avoid injection; store MIME type so browsers render correctly; use MEDIUMBLOB/LONGBLOB for large files; if you expect very high traffic consider storing files on disk or a CDN and saving paths in the DB (that’s the route suggested); avoid the temp-file approach () for per-request serving because of cleanup and concurrency issues.
Jump to Post— kvprajapati 1,826Please do not resurrect old threads. If you have any questions please ask. .... You are welcome to start your own threads.
Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.
Thread Closed.
I have the problem too. I searched but not found any solution. plz guid me
(^_^)
You should save the image path in the table. Then retrieve from it.
get Byte Array from DB then convert it into image and save it into some folder on serverside then use the URL of that image to represent it
eg:-
imageSample.imageURL=~URLofYourImage;
CODE:-
public void byteArrayToImage(byte[] byteArrayIn)
{
try
{
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
imgPhoto.Save(Server.MapPath("temp.jpg"));
imgEmployee.ImageUrl = "~/Employee/temp.jpg";
}
catch (Exception ex)
{
imgEmployee.ImageUrl = "~/Admin/emp1.jpg";
ExceptionLogger.PublishError(ex);
}
} Please do not resurrect old threads. If you have any questions please ask. .... You are welcome to start your own threads.
Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.
Thread Closed.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.