Image-Saving and Reading

Save and Read Image From Database

You can use the following SQL statement to create a table that can store image files:

Description of Table Attributes:

Id(Primary Key): To store the id of the Image.

ImageTitle: To store Image Title.

ImageContent: To store Binary Image data.

ImageType: To store the content type of the image,Ie whether it is JPEG or BMP or any other

Steps that has to be followed:

1.Get the content length of the Image that is to be uploaded
2. Create a byte array to store that Image and length of the array should be the content length of the image
3. Read the input stream of the posted file

Code Snippet

if (fluPhotoUpload .HasFile)
            Stream strFileUp = fluPhotoUpload.PostedFile.InputStream;
            byte[] imageData = new byte[strFileUp.Length];

            strFileUp.Read(imageData, 0, imageData.Length);


Retrieve image from database

If we try to view Image column in the backend, you will notice the data in the image shows “binary” and will not let you view the bytes. The only way to view the image bytes is to retrieve the binary stream by reading the byte array from the table.

Code Snippet

     byte[] img = (byte[])objcls.SQlExecuteScalar("Select imagecontent from SaveImage where  imagetitle='" + txtTitle.Text + "'");
        if (img != null)
            Response.ContentType = "image/gif";


2. Saving Image in a folder in the Application

Here we l create a folder by the name “Image “in the application using code.
All the images we upload using fileuploader are saved in this folder.lets see how it s been done.

Step1.create a folder inside the application folder by the name “Images”.

Msdn Definition

“The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server

Code Snippet

  String Subdirectory = Server.MapPath(".");
  String Maindirectory = Server.MapPath("~");

Here in string variable Subdirectory shows the subdirectory path , and string variable Maindirectory shows the current directory
To create Folder by the name Image in the application folder. we can use the given code.

Code Snippet

using System.IO;
 if (Directory.Exists(Subdirectory + "\\images"))
               Response.Write("Directory exists already.");

               Directory.CreateDirectory(Subdirectory + "\\images");

We have created a folder by the name Image. this is the folder where we upload images.

Uploading image to this folder

Code Snippet

string filename = Path.GetFileName(fluPhotoUpload.PostedFile.FileName);

        fluPhotoUpload.PostedFile.SaveAs(Maindirectory +"\\images" + "/" + filename);

DataBase Structure

Save image name in the back end using executeNonQuery

Retrieving the image and dispalying it in the image control

Code Snippet

      imgFileup.ImageUrl = "images/" + ImageName;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: