convert uploaded file to bitmap image in asp.net

19,728

You can get a bitmap of your uploaded image as follows:

System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(userFileUpload.PostedFile.InputStream);

You then get the stored copy (which hopefully is stored as a byte array and you have an ID to get it), and then convert it to bitmap as follows

byte[] byteArrayStoredImage = ImageService.GetImageData(imageID);
MemoryStream imgStream = new MemoryStream(byteArrayStoredImage);
System.Drawing.Bitmap bmpStoredImage = new Bitmap(imgStream);

With the two bitmaps in hand (bmpPostedImage and bmpStoredImage), you can then call the function to do the comparison. For a start you can try this function from http://www.dreamincode.net/code/snippet2859.htm and see how it it goes. there may be more efficient functions out there to do the comparison, trying out a google search for that will be a goo thing to try.

EDIT

Find below the code to retrieve image from the database with the assumptions I put in the comment below:

    public byte[] GetImageData(string imageID)
    {
                string connectionString = ConfigurationManager.ConnectionStrings["connectionstringname"];
        SqlConnection connection = SqlConnection(connectionString);
        connection.Open();
        SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@imageId", connection);
        SqlParameter myparam = command1.Parameters.Add("@imageId", SqlDbType.NVarChar, 30);
        myparam.Value = imageID;
        byte[] img = (byte[])command1.ExecuteScalar();
        connection.Close();
        return img;
    }

and then change ImageService.GetImageData(imageID) to GetImageData(imageID);

Note also, that error handling is not addressed here, so might have to factor that into your final code.

Share:
19,728
Piyush
Author by

Piyush

Updated on June 12, 2022

Comments

  • Piyush
    Piyush about 2 years

    I have a FileUpload box and button. In my scenario the files that are going to be uploaded are image files. I want to convert these image files into bitmaps and store them temporarily in a buffer.

    I have a function which takes two bitmap inputs and tells us whether the two files match or not.

    One of the file will come from the FileUpload control on ButtonClick event and the other bitmap will be read from Database.

    Can anyone tell me as to how do I convert these files to bitmaps and pass both the bitmap objects to the function.

  • Piyush
    Piyush about 13 years
    ImageService.GetImageData() does not exist. What should I do. I have stored the image in image datatype in the database.
  • Gboyega Sulaiman
    Gboyega Sulaiman about 13 years
    @Piyush: Of course ImageService.GetImageData(imageID) will have to be replaced by your own code that you use to connect to your database to retrieve the image, but which returns a byte array of the image data.
  • Piyush
    Piyush about 13 years
    can you help me with retrieving an image stored in database as an image datatype and converting it into byte[] array.
  • Piyush
    Piyush about 13 years
    I am trying to use ToArray() but this is not working for me.
  • Gboyega Sulaiman
    Gboyega Sulaiman about 13 years
    @Piyush: I assume you are familiar with ADO.NET, and that you are using SQL Server. The sample code from here (beansoftware.com/ASP.NET-Tutorials/…) will put you on the right track. I will edit my answer to put short and direct way to accomplish this however.
  • Piyush
    Piyush about 13 years
    I am using var image = (from c in db.Citizen_tables select c.FingerPrint).ToList(); code to get all the fingerprint images and now querying through the resultset and converting each element into byte[] by the following code byte[] arr = image.ElementAt(i).ToArray(); where i is a looping variable.
  • Gboyega Sulaiman
    Gboyega Sulaiman about 13 years
    @Piyush: this is another dimension, One, I didn't realise you are using EF and secondly, trying to pull all images in the database to memory and then work through them will be inefficient. Concerning the code you posted, to access each image item use byte[] arr = image[i];
  • Piyush
    Piyush about 13 years
    Thanks a lot sir, this really helped.