Loading ...

How to save & retrieve image from Mysql DB ?

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  data access   » How to save & retrieve image from Mysql DB ?

How to save & retrieve image from Mysql DB ?

Posts under the topic: How to save & retrieve image from Mysql DB ?

Posted: 6/20/2011

Lurker 245  points  Lurker
  • Joined on: 4/15/2011
  • Posts: 44

How to  save & retrieve image from Mysql DB ?

actually My image is get stored but i am not able to retrieve it

Please help.......


Posted: 6/20/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74

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

 

 


tags DataAccess

Posted: 6/21/2011

Lurker 245  points  Lurker
  • Joined on: 4/15/2011
  • Posts: 44

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);


Posted: 6/21/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74

Hi Pallavi,

Try this:

ImageConverter imageConverter = new System.Drawing.ImageConverter();
Image image = imageConverter.ConvertFrom(byteArray) as Image;


Best Regards,

Gjorgji Dimitrov


Posted: 6/22/2011

Lurker 245  points  Lurker
  • Joined on: 4/15/2011
  • Posts: 44

hi Gjorgji,

 it's still  not working fetching the same error i.e

System.ArgumentException: Parameter is not valid.


Posted: 6/26/2011

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

pallavi karpe said:

hi Gjorgji,

 it's still  not working fetching the same error i.e

System.ArgumentException: Parameter is not valid.

Can you show us the complete code you use?

Thanks,
Hajan


Page 1 of 1 (6 items)