DBA > Articles

Inserting Image into Database in C#.NET Window Application

By: Sourabh Sharma On
To read more DBA articles, visit http://dba.fyicenter.com/article/

Images are mostely used to recognize identification of any person or firm and while developing any Window Application you need to use Images many times within you Application. And also we need to save that Images into our Database so that we can access that images and use them when required. The one way to save Image inDatabase is inserting the location of Image, but this method is not efficient. Because this method generates problem some time.

In this article, I am going to tell you that how to store Image(not the location of Image) into Database directly from our Window Application Form. Inserting the whole Image is the most efficient way and also faster method to interact with database.

Procedure
To insert an Image into Database table we need to follow the following steps:-

Step 1: Browse an Image from system hard disk and show the image in Picturebox control.

Step 2: Convert the image into byte[] array so we can store this image into the database table.

step 3: Pass this image with SQL query to store it into Database.

Namespaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Drawing.Imaging;

Code Page
Step 1: Browse an Image from system hard disk and show the image in Picturebox control.

private void btnUploadImage_Click(object sender, EventArgs e)
{
try
{

// create an object of OpenFileDialog to browse image

OpenFileDialog open = new OpenFileDialog();

// filter the dialogbox so that user can select only some specific files from hard disk

open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

//if user has selected any file then do the following

if (open.ShowDialog() == DialogResult.OK)
{
//create an object of Image class and assign the image name

Image img = new Bitmap(open.FileName);

// show the image in Picrutebox control

pcPhoto.Image = img.GetThumbnailImage(340, 165, null, new IntPtr());

open.RestoreDirectory = true;
}
}
catch
{
MessageBox.Show("Can not upload image");
}
}


Step 2: Convert the image into byte[] array so we can store this image into the database table.

private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (Validate() && convertImage())
{

insertImage();
}

}
catch
{
}
}
public bool Validate()
{
try
{
if (Convert.ToInt32(txtid.Text.Trim()) == 0)
{
MessageBox.Show("Please enter valid id");
return false;
}
else
return true;
}
catch
{
MessageBox.Show("Please enter valid id");
return false;
}
}
public bool convertImage()
{
try
{

//create an object of MemoryStream class

MemoryStream ms = new MemoryStream();
//save the image into memory stream
pcPhoto.Image.Save(ms, ImageFormat.Jpeg);
//assign the byte array with total size of memorystream

photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo, 0, photo.Length);
return true;
}
catch
{
MessageBox.Show("image can not be converted");
return false;
}
}

Full article...


Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/