Saturday, January 30, 2010

Image uploading in Asp.net



Image uploading using Asp.net to MS sql server by c# is very easier.Here i will show how to convert an image to binary data and reduce the image size and then upload the binary data to MS sql server.

Create the Database structure

In the database we have to declare the image id, image name and image binary data.

name type
imageId int identity not null
imageName varchar(100)
imageBinaryData image

if we declare the imageBinaryData as image means.we can convert our image to binary data and the upload to the sql server is easier.


In the New Web Form page drag and drop the Fileupload control and name it as FileUpload and a button control as btnSubmit.

Write the logic for file upload and resize the image in .cs file

use the Name space on the top of the namespace declaration.

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

then declare the sql connection before the page load event.

SqlConnection Conn = new SqlConnection(“server=.;database=sample;Integrated Security=True”);

in the button click event
//Check the file upload is having some data.
if (FileUpload.FileName != “”)
{
System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload.PostedFile.InputStream);
int imgHeight, imgWidth;
imgHeight = 130;
imgWidth = 150;

///….
}

I will attach the sample Image upload File with this.

3 comments:

  1. this is very good post which is providing the information about image uploading...
    thank you..

    ReplyDelete
  2. Awesome blog man, the things you have mentioned above are really informative and are examples of your awesome writing skills and a good blog.
    .net Obfuscator

    ReplyDelete
  3. It's an excellent information. Thanks for sharing, keep updating.

    Best Mobility Services | Austere Technologies

    ReplyDelete

Followers