Thursday, 14 August 2014

C#.NET Save and Retrieve Image From Database. ( Part 5 – Retrieve image from database)



This is the final part of this project. In this part I will show you how to retrieve the saved image from database and view it in the picturebox. At the end we will also focus on, how to delete the image from database.
We have a listBox that populates all saved image’s name. And you know imageName is the primary key of our SavedImages table. So it is unique. We will gather image data querying by imagename. Listbox refreshes the image list at page load event and save button click event. One more time it will be refreshed when we will delete any image.
In bthShow_Click event we will use sqlDataReader. Our code for btnShow_click event

private void bthShow_Click(object sender, EventArgs e)

        {

            SqlConnection Conn = new SqlConnection(GenerateString());



            if (Conn.State == ConnectionState.Closed)

            {

                Conn.Open();

            }

            SqlCommand cmd = new SqlCommand();

            cmd.Connection = Conn;

            try

            {

                SqlDataReader reader;

                cmd.CommandText = "SELECT     ImageData FROM         SavedImages where ImageName='" + listImageName.Text + "'";

                reader = cmd.ExecuteReader();

                while (reader.Read())

                {

                    GetImage(pictureBox1, (byte[])reader["ImageData"]);

                }

                reader.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

Don’t be confused with GetImage().  It is a function. I would like to remind you that we have saved image to database using bytes arrey. Now to retrieve the image we have to convert the bytes into image. Then we will be able to show it in picturebox. To convert byte arrey into image data and view it in picturebox I  have used GetImage()  function.

public static void GetImage(PictureBox PictureImage, Byte[] ImageValue)

        {



            try

            {

               

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

            }



        }


Now just select any imagename and click on Show button. That’s it.
If you want to show image by selectedIndexChange event of the listBox, just use the code. Just call the     bthShow_Click event. It will work like a function.

private void listImageName_SelectedIndexChanged(object sender, EventArgs e)

        {

            bthShow_Click(sender, e);

        }

Image delete :

Select an image name from the listbox and click on the deletebutton. The code for image deletion is simple.  

  private void btnDelete_Click(object sender, EventArgs e)

        {

            if (MessageBox.Show("Do you want to delete this image?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) return;

            try

                {

                    SqlCommandExecute("DELETE FROM SavedImages WHERE ImageName ='" + listImageName.Text + "'");

                    MessageBox.Show("Image deleted successfully.","Deleted");

                    frmImageSave_Load(sender, e);

                }

            catch(Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

        }


Here is the whole project in PDF format. 

No comments:

Post a Comment