Thursday, September 18, 2008

Image Upload Web Service in VB.NET

Image Upload Web Service in VB.NET


We are going to be doing and in house pictorial directory here at Chapel Hill this fall. I want this directory to be integrated with our website and allow us to view the directory pictures online.

The process as I envision it would include a family taking some time out of their Sunday or Wednesday night to have a picture taken. The photographer would take a family picture as well as individual pictures and transfer these to a computer. It would be ideal if the pictures transfered as they were being taken. We would then have a piece of software that would allow us to look up the info we have for this family, update it if needed, and upload pictures for the family and for each family member.

This software will need to re-size the images down from the hi-quality images taken by the camera to something that is a web appropriate size, upload the image, and update the family / individual profile to indicate which image belongs to them.

In order to do this, I needed a way to upload an image to a website using a VB program. I decided I would use a web service to do this and through some Google research I was able to put together a web service to do this.

Here is the code I used for the web service with a web form to test it out.
When I have finished the vb program I will post that code.

First the web service
-----------------------------------------------
Public Function UploadImage(ByVal imgdata() As Byte, ByVal FileName As String) As Integer
'Find the path on the server of our apps images folder
Dim FilePath As String = Server.MapPath("images")

'strip the path and .jpg etc out of our filename
Dim i As Integer = InStrRev(FileName, "\")
FileName = Mid(FileName, i)
Dim j As Integer = InStr(FileName, ".") - 1
Dim k As Integer = Len(FileName)
FileName = Mid(FileName, 1, j)

'Create a memory stream to hold the image data
Dim MS As New System.IO.MemoryStream(imgdata)

'Convert the memory stream to a bitmap image
Dim BM As New System.Drawing.Bitmap(Image.FromStream(MS))

'Convert the bitmap to a png and save it in our images folder
BM.Save(FilePath & FileName & ".png", Imaging.ImageFormat.Png)

'eventually we will create a database entry for this image and return the image ID
Return 1
End Function

---------------------------------------------------

Now the VB code for the web form
------------------------------------------------
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ws As New localhost.ImageUpload
Dim fs As Stream = FileUpload1.PostedFile.InputStream
Dim img(fs.Length) As Byte
fs.Read(img, 0, Convert.ToInt32(fs.Length))

ws.UploadImage(img, FileUpload1.PostedFile.FileName)
End Sub
End Class

---------------------------------------------------------

I hope someone finds this useful.

2 comments:

Madeline AJY said...

HI! I need help for my IT project:/ It is ok, if you send me your codes for uploading image? Please!
madeline_90@hotmail.com
xoxo

2009 DREAM said...

This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. check my webcam