Posted: 6/20/2011
How to save & retrieve image from Mysql DB ?
actually My image is get stored but i am not able to retrieve it
Please help.......
Hi Pallavi,
I'm not sure how you store the image in MySql DB, but you need to use BLOB which is for binary data, datatype should be 'longblob'.
Here is a test code that works for me:
public class DataAccess { private string _strConn = @"Driver= {MySQLODBC x.xx Driver};SERVER=localhost;DATABASE=test;"; private OdbcConnection _objConn; public DataAccess() { this._objConn = new OdbcConnection(this._strConn); } // This function adds the Images to database public string addImage(byte [] buffer,string extension) { string strSql = "SELECT * FROM File"; DataSet ds = new DataSet("Image"); OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn); OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP); tempAP.Fill(ds,"Table"); try { this._objConn.Open(); DataRow objNewRow = ds.Tables["Table"].NewRow(); objNewRow["Extension"] = extension; objNewRow["Data"] = buffer; ds.Tables["Table"].Rows.Add(objNewRow); // trying to update the table to add the image tempAP.Update(ds,"Table"); } catch(Exception e){return e.Message;} finally{this._objConn.Close();} return null; } // This function to get the image data from the database public byte [] getImage(int imageNumber) { string strSql = "SELECT * FROM File"; DataSet ds = new DataSet("Image"); OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn); OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP); tempAP.Fill(ds,"Table"); try { this._objConn.Open(); byte [] buffer = (byte [])ds.Tables["Table"].Rows[imageNumber]["Data"]; return buffer; } catch{this._objConn.Close();return null;} finally{this._objConn.Close();} } // Get the image count public int getCount() { string strSql = "SELECT COUNT(Data) FROM File"; DataSet ds = new DataSet("Image"); OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn); OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP); tempAP.Fill(ds,"Table"); try> { this._objConn.Open(); int count = (int)ds.Tables["Table"].Rows[0][0]; return count; } catch{this._objConn.Close();return 0;} finally{this._objConn.Close();} } }
Here is the view page for displaying the image:
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Data.DataAccess data = new Data.DataAccess(); int imagenumber = 0; try { imagenumber = int.Parse(Request.QueryString["image"]); } catch(System.ArgumentNullException ee) { imagenumber = 0; } byte [] buffer = data.getImage(imagenumber); System.IO.MemoryStream stream1 = new System.IO.MemoryStream(buffer,true); stream1.Write(buffer,0,buffer.Length); Bitmap m_bitmap = (Bitmap) Bitmap.FromStream(stream1,true); Response.ContentType = "Image/jpeg"; m_bitmap.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); }
The Id of the image is a query string parameter 'image':
http://localhost/MySqlFileManager/View.aspx?image=220
You can download the whole sample from:
http://www.codeproject.com/KB/aspnet/image_asp.aspx
Best Regards,
Gjorgji Dimitrov
Posted: 6/21/2011
Hi Gjorgji,
I am also using this code & i have long blobe datatype to store an image
but i am getting an error i.e Parameter is not valid
at line Bitmap m_bitmap = (Bitmap) Bitmap.FromStream(stream1,true);
Try this:
ImageConverter imageConverter = new System.Drawing.ImageConverter(); Image image = imageConverter.ConvertFrom(byteArray) as Image;
Posted: 6/22/2011
hi Gjorgji,
it's still not working fetching the same error i.e
System.ArgumentException: Parameter is not valid.
Posted: 6/26/2011
pallavi karpe said: hi Gjorgji, it's still not working fetching the same error i.eSystem.ArgumentException: Parameter is not valid.
Can you show us the complete code you use?
Thanks,Hajan