You must Sign In to post a response.
  • Category: Visual Studio

    How to upload and show an image from the disk in VS

    Sir i m developing a project. In that project a registration form in which i want to upload the user's photo from the disk and i also want that it has upload on my database and after that when i search the student on search.aspx it shows on their.
  • #752782
    You can make use of File upload control to provide the option to user for uploading a file.


    once the file is uploaded please use the below code to store that file in data base.



    private void cmdSave_Click(object sender, EventArgs e)
    {
    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();
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #752790
    hi sachin

    please refer the url explaining how to store images into sql server database and display. it will help you on this.

    http://dotnetgallery.com/kb/resource21-How-to-store-and-retrieve-images-from-SQL-server-database-using-aspnet.aspx

  • #752797
    Hi,

    Please refer below code to solve your issues:

    ***Code for Image Browsing***
    private void btnBrowse_Click(object sender, EventArgs e)
    {
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "Image File (*.jpg;*.bmp;)|*.jpg;*.bmp;";
    DialogResult dlgRes = dlg.ShowDialog();
    if (dlgRes != DialogResult.Cancel)
    {
    pictureBox1.ImageLocation = dlg.FileName;
    txtImagePath.Text = dlg.FileName;
    }
    }

    ***Function for Read Image***
    public static 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;
    }

    ***Function for Retreive Image***
    public static void StoreImage(PictureBox PictureImage, Byte[] ImageValue)
    {
    try
    {
    //Get image data from gridview column.
    byte[] imageData = (byte[])ImageValue;

    //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
    PictureImage.Image = newImage;
    //return PictureImage;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }

    ***Code for Save Image into Database***
    private void btnSave_Click(object sender, EventArgs e)
    {
    try
    {
    if (txtImagePath.Text != string.Empty)
    {
    byte[] imageData = PayrollSystem.GlobalInfo.GlobalInfo.ReadFile(txtImagePath.Text);
    Comm.Parameters.Add(new SqlParameter("@ImageData", (object)imageData));
    }
    Comm.CommandText = "INSERT INTO Emp_Info (EmpName, EmpImage) VALUES ('" + txtEmpName.Text.ToString().ToUpper() + "',@ImageData)";
    Comm.ExecuteNonQuery();
    MessageBox.Show("Saved Successfully.", "Saved");
    txtEmpName.Text = "";
    pictureBox1.Image = null;
    SqlTrans.Commit();
    Cursor = Cursors.Default;
    }
    catch (Exception ex)
    {
    SqlTrans.Rollback();
    MessageBox.Show(ex.Message);
    Cursor = Cursors.Default;
    }
    }

    ***Code for Retreive Image***
    private void cboEmpName_SelectedIndexChanged(object sender, EventArgs e)
    {
    pictureBox1.Image = null;
    string sql = "Select * From Emp_Info Where EmpName ='" + cboEmpName.Text + "'";
    reader = GlobalInfo.GlobalInfo.returnReader(sql);
    while (reader.Read())
    {
    if (!reader.IsDBNull(3))
    {
    PayrollSystem.GlobalInfo.GlobalInfo.StoreImage(pictureBox1, (byte[])reader["EmpImage"]);

    }
    }
    }

    Hope this will help you.

    Thanks,
    Ram Prasad

  • #752839
    Sir can u please attach a zip.


  • Sign In to post your comments