Results 1 to 3 of 3

Thread: C# Storing Images

  1. #1
    Junior Member
    Join Date
    May 2008
    Answers
    1

    C# Storing Images

    How to store image in DB and how to retrieve it from DB into PictureBox....

    Hi,
    i hv empid,empname,empphot in my Form...now wen i run the appln..i want to get these details...thn how can i get the image of the emp from DB.
    So,How can i store image into DB and how can i retrive it from DB into PictureBox...
    my DB is SQLSERVER 2005 and i want it in C# windows application....

    plzz help me......

    Advance Thnxxxxx


  2. #2
    Junior Member
    Join Date
    May 2008
    Answers
    8

    Re: C# Storing Images

    After getting the image path follow this : private void upload() { try { //read image bytes into a byte array byte[] imagedata = readfile(txtimagepath.text); //initialize sql server connection sqlconnection cn = new sqlconnection(txtconnectionstring.text); //set insert query string qry = "insert into imagesstore (originalpath,imagedata) values(@originalpath, @imagedata)"; //initialize sqlcommand object for insert. Sqlcommand sqlcom = new sqlcommand(qry, cn); //we are passing original image path and image byte data as sql parameters. Sqlcom.parameters.add(new sqlparameter("@originalpath", (object)txtimagepath.text)); sqlcom.parameters.add(new sqlparameter("@imagedata", (object)imagedata)); //open connection and execute insert query. Cn.open(); sqlcom.executenonquery(); cn.close(); //close form and return to list or images. This.close(); } catch(exception ex) { messagebox.show(ex.tostring()); } } //open file in to a filestream and read data in a byte array. Byte[] readfile(string spath) { //initialize byte array with a null value initially. Byte[] data = null; //use fileinfo object to get file size. Fileinfo finfo = new fileinfo(spath); long numbytes = finfo.length; //open filestream to read file filestream fstream = new filestream(spath, filemode.open, fileaccess.read); //use binaryreader to read file stream into byte array. Binaryreader br = new binaryreader(fstream); //when you use binaryreader, you need to supply number of bytes to read from file. //in this case we want to read entire file. So supplying total number of bytes. Data = br.readbytes((int)numbytes); return data; } //get the image from database. Private void getimagesfromdatabase() { try { sqlconnection cn = new sqlconnection(txtconnectionstring.text); //initialize sql adapter. Sqldataadapter adap = new sqldataadapter("select image from imagesstore", cn); //initialize dataset. Dataset ds = new dataset(); //fill dataset with imagesstore table. Adap.fill(ds, "imagesstore"); //fill picturebox with image. Byte[] imagedata = (byte[])ds.tables["imagesstore"].rows[0]["imagedata"].tostring(); //initialize image variable image newimage; //read image data into a memory stream using (memorystream ms = new memorystream(imagedata, 0, imagedata.length)) { ms.write(imagedata, 0, imagedata.length); //set image variable value using memory stream. Newimage = image.fromstream(ms, true); } //set picture picturebox1.image = newimage; } catch(exception ex) { messagebox.show(ex.tostring()); } } let me know if you face any problems.


  3. #3
    Junior Member
    Join Date
    May 2008
    Answers
    8

    Re: C# Storing Images

    After getting the image path follow this :
    private void upload()
    {
    try
    {
    //read image bytes into a byte array
    byte[] imagedata = readfile(txtimagepath.text);
    //initialize sql server connection
    sqlconnection cn = new sqlconnection(txtconnectionstring.text);
    //set insert query string qry = "insert into imagesstore (originalpath,imagedata) values(@originalpath, @imagedata)";
    //initialize sqlcommand object for insert.
    Sqlcommand sqlcom = new sqlcommand(qry, cn);
    //we are passing original image path and image byte data as sql parameters. Sqlcom.parameters.add(new sqlparameter("@originalpath", (object)txtimagepath.text)); sqlcom.parameters.add(new sqlparameter("@imagedata", (object)imagedata));
    //open connection and execute insert query.
    Cn.open();
    sqlcom.executenonquery();
    cn.close();
    //close form and return to list or images.
    This.close(); }
    catch(exception ex)
    {
    messagebox.show(ex.tostring());
    }
    }
    //open file in to a filestream and read data in a byte array.
    Byte[] readfile(string spath)
    {
    //initialize byte array with a null value initially.
    Byte[] data = null;
    //use fileinfo object to get file size.
    Fileinfo finfo = new fileinfo(spath);
    long numbytes = finfo.length;
    //open filestream to read file
    filestream fstream = new filestream(spath, filemode.open, fileaccess.read);
    //use binaryreader to read file stream into byte array.
    Binaryreader br = new binaryreader(fstream);
    //when you use binaryreader, you need to supply number of bytes to read from file.
    //in this case we want to read entire file. So supplying total number of bytes.
    Data = br.readbytes((int)numbytes);
    return data;
    }

    //get the image from database.
    Private void getimagesfromdatabase()
    {
    try
    {
    SqlConnection CN = new SqlConnection(txtConnectionString.Text);

    //Initialize SQL adapter.
    SqlDataAdapter ADAP = new SqlDataAdapter("Select Image from ImagesStore", CN);

    //Initialize Dataset.
    DataSet DS = new DataSet();

    //Fill dataset with ImagesStore table.
    ADAP.Fill(DS, "ImagesStore");

    //Fill picturebox with image.
    byte[] imageData = (byte[])DS.Tables["ImagesStore"].Rows[0]["ImageData"].ToString();

    //Initialize image variable
    Image newImage;
    //Read image data into a memory stream
    using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
    {
    ms.Write(imageData, 0, imageData.Length);

    //Set image variable value using memory stream.
    newImage = Image.FromStream(ms, true);
    }

    //set picture
    pictureBox1.Image = newImage;
    }
    catch(exception ex)
    {
    messagebox.show(ex.tostring());
    }
    }

    let me know if you face any problems.


  4.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact