Here is some code that I have been using to save my images into a MS Access Database with ASPX vb.net.
The html code looks like this. It is to upload a picture and add title and description to it.
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <h1> Image Upload Information</h1> <p style="text-align: left"> <table> <tr> <td style="width: 100px"> &nbsp; Image :&nbsp;</td> <td style="width: 100px"> <asp:FileUpload ID="FileUpload1" runat="server" /></td> </tr> <tr> <td style="width: 100px"> &nbsp; Title &nbsp;&nbsp; :&nbsp;</td> <td style="width: 100px"> <asp:TextBox ID="Tbtitle" runat="server" Width="376px">Title Here</asp:TextBox></td> </tr> <tr> <td style="width: 100px"> &nbsp; Description :&nbsp;</td> <td style="width: 100px"> <asp:TextBox ID="description" runat="server" Height="88px" TextMode="MultiLine" Width="384px">Description Here</asp:TextBox></td> </tr> <tr> <td style="width: 100px"> &nbsp;Location :&nbsp;</td> <td style="width: 100px"> <asp:TextBox ID="location" runat="server" Width="376px">Location Here</asp:TextBox></td> </tr> <tr> <td style="width: 100px"> &nbsp; Tags :&nbsp;</td> <td style="width: 100px"> <asp:TextBox ID="tags" runat="server" Width="376px">Tags Here</asp:TextBox></td> </tr> <tr> <td style="width: 100px"> &nbsp;Width :&nbsp;<br /> </td> <td style="width: 100px"> <asp:TextBox ID="width" runat="server" Width="40px">1024</asp:TextBox></td> </tr> <tr> <td style="width: 100px"> &nbsp;&nbsp; Height :&nbsp;</td> <td style="width: 100px"> <asp:TextBox ID="height" runat="server" Width="40px">768</asp:TextBox></td> </tr> <tr> <td style="width: 100px"> </td> <td style="width: 100px"> </td> </tr> <tr> <td style="width: 100px"> </td> <td style="width: 100px"> <asp:Button ID="Button1" runat="server" Text="Upload Image" /></td> </tr> </table> <br /> &nbsp; <br /> <br /> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </p> </asp:Content>
In the code behind page the following code is there. When you click the button it will upload the image to the MS Access Database and it will show the image on the page in the literal1
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim wService As com.www.imgStore = New com.www.imgStore Dim myFile As HttpPostedFile = FileUpload1.PostedFile Dim nFileLen As Integer = myFile.ContentLength Dim myData(nFileLen) As Byte myFile.InputStream.Read(myData, 0, nFileLen) Dim ms As IO.MemoryStream = New IO.MemoryStream(myData) Dim img As Drawing.Image = convertImg(ms) img = resizeImage(img, width.Text.Trim & "x" & height.Text.Trim) Dim bytes() As Byte = imgToBytes(img) Dim ret As String = wService.storeImage(Tbtitle.Text, description.Text, location.Text, tags.Text, width.Text, height.Text, Date.Now, bytes) Literal1.Text = "http://www.yoursite.com/imgGrabing.aspx?id=" & ret & "<br/>" & bytes.Length & " Bytes Uploaded<br/><img src=""imgGrabing.aspx?id=" & ret & """>" wService.Dispose() ms.Dispose() img.Dispose() End Sub
This is the code to convert the image.
Public Function convertImg(ByVal imgL As IO.MemoryStream) As System.Drawing.Image Dim memstream As IO.MemoryStream = New IO.MemoryStream Dim icf() As Drawing.Imaging.ImageCodecInfo = Drawing.Imaging.ImageCodecInfo.GetImageEncoders Dim encps As Drawing.Imaging.EncoderParameters = New Drawing.Imaging.EncoderParameters(1) encps.Param(0) = New Drawing.Imaging.EncoderParameter(Drawing.Imaging.Encoder.Quality, 100) Dim btmp As Drawing.Bitmap = Drawing.Bitmap.FromStream(imgL) btmp.Save(memstream, icf(1), encps) btmp.Dispose() btmp = Nothing Dim img As Drawing.Image = Drawing.Bitmap.FromStream(memstream) Return img End Function
Move the image over into a Byte() is done here:
Public Function imgToBytes(ByVal bmp As Drawing.Image) As Byte() Dim ms As New IO.MemoryStream bmp.Save(ms, Drawing.Imaging.ImageFormat.Jpeg) Dim abyt(ms.Length - 1) As Byte ms.Seek(0, IO.SeekOrigin.Begin) ms.Read(abyt, 0, ms.Length) Return abyt End Function
When I save the image, I resize it to a decent size to store in the database, so that I dont have to worry about resizing the images first before uploading.
Public Function resizeImage(ByVal img As Drawing.Image, ByVal size As String) As Drawing.Image
Dim oImg As Drawing.Image = img
Dim width As Integer = size.Split("x")(0)
Dim height As Integer = size.Split("x")(1)
Dim oThumbNail As Drawing.Image = New Drawing.Bitmap(width, height, oImg.PixelFormat)
Dim oGraphic As Drawing.Graphics = Drawing.Graphics.FromImage(oThumbNail)
oGraphic.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
oGraphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
oGraphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
Dim oRectangle As Drawing.Rectangle = New Drawing.Rectangle(0, 0, oThumbNail.Width, oThumbNail.Height)
oGraphic.DrawImage(oImg, oRectangle)
oImg.Dispose()
Return oThumbNail
End Function
I have created a webservice to save the images, because I use it also to upload still images from my PTZ camera and it just connects to the webservice and stores the images.
<WebMethod()> _
Public Function storeImage(ByVal cameraid As Integer, ByVal preset As Integer, ByVal bits() As Byte) As Boolean
saveImg(cameraid, preset, Date.Now, bits)
Return True
End Function
Public Function saveImg(ByVal Cameraid As Integer, ByVal preset As Integer, ByVal picTimeStamp As Date, ByVal picImg As Byte()) As Boolean
Try
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\database.mdb"
Dim oleDBConn As New Data.OleDb.OleDbConnection(strConn)
oleDBConn.Open()
Dim oleDBAdapter As Data.OleDb.OleDbDataAdapter = New Data.OleDb.OleDbDataAdapter("Select * From Pictures", strConn)
Dim oleDBcb As Data.OleDb.OleDbCommandBuilder = New Data.OleDb.OleDbCommandBuilder(oleDBAdapter)
Dim ds As Data.DataSet = New Data.DataSet
oleDBAdapter.MissingSchemaAction = Data.MissingSchemaAction.AddWithKey
oleDBAdapter.Fill(ds, "Pictures")
Dim dsRow As Data.DataRow = ds.Tables(0).NewRow
dsRow.BeginEdit()
dsRow("Cameraid") = Cameraid
dsRow("Preset") = preset
dsRow("picTimeStamp") = picTimeStamp
dsRow("picBinary") = picImg
dsRow.EndEdit()
ds.Tables(0).Rows.Add(dsRow)
oleDBAdapter.Update(ds.Tables(0))
ds.AcceptChanges()
oleDBConn.Dispose()
oleDBcb.Dispose()
oleDBAdapter.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
The Access Database structure I used is:
ImgID:AutoNumber
PicTimeStamp: Date/Time
picBinary:Ole Object
CameraID:Number
Preset:Number
Hope this will get you going in the right direction and start to save your Images to Database using vb.net and MS Access













