troubleshooting Question

Help with watermark for uploading image to Access DB

Avatar of tjgrindsted
tjgrindsted asked on
ASP.NET
5 Comments1 Solution168 ViewsLast Modified:
Hi i have a code that can upload an image to an access db, what i want to do now is to add a watermark.png to the image and the upload the image to the access db.
i cant get it to work...

My code is:
Imports System.Data 
Imports System.Data.OleDb 
Imports System.IO 
 
Public Class watermark 
    Inherits System.Web.UI.Page 
 
    ' Access Database oledb connection string 
    ' Using Provider Microsoft.Jet.OLEDB.4.0 
    Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/db1.mdb") 
 
    ' Object created for Oledb Connection 
    Dim myAccessConnection As New OleDbConnection(connStr) 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
        If Not IsPostBack Then 
 
        End If 
    End Sub 
 
    Public Sub openAccessConnection() 
        ' If condition that can be used to check the access database connection 
        ' whether it is already open or not. 
        If myAccessConnection.State = ConnectionState.Closed Then 
            myAccessConnection.Open() 
        End If 
    End Sub 
 
    Public Sub closeAccessConnection() 
        ' If condition to check the access database connection state 
        ' If it is open then close it. 
        If myAccessConnection.State = ConnectionState.Open Then 
            myAccessConnection.Close() 
        End If 
    End Sub 
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Try 
            openAccessConnection() 
 
            Dim imageSize As Int64 
            Dim imageType As String 
            Dim imageStream As Stream 
 
            ' Gets the Size of the Image 
            imageSize = fileImgUpload.PostedFile.ContentLength 
 
            ' Gets the Image Type 
            imageType = fileImgUpload.PostedFile.ContentType 
 
            ' Reads the Image stream 
            imageStream = fileImgUpload.PostedFile.InputStream 
 
            Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(imageStream) ' your main file   
            Dim wimg As System.Drawing.Image = System.Drawing.Image.FromFile("img/watermark.png") ' your watermark file  
            Dim FinalPicStream As MemoryStream = New MemoryStream() 
 
            Using (img) 
                Using bitmap As New Bitmap(width, height) 
                    Using canvas As Graphics = Graphics.FromImage(bitmap) 
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic 
                        canvas.DrawImage(img, New Rectangle(0, 0, width, height), New Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel) 
                        canvas.DrawImage(wimg, (bitmap.Width / 2) - (wimg.Width / 2 + 5), (bitmap.Height / 2) - (wimg.Height / 2 + 5)) 
                        canvas.Save() 
                    End Using 
                    ' Save to memory using the Jpeg format  
                    bitmap.Save(FinalPicStream, ImageFormat.Jpeg) ' or your prefered format  
                    ' again you got the stream with watermarked image and can save to database  
                End Using 
            End Using 
 
            Dim imageContent(imageSize) As Byte 
            Dim intStatus As Integer 
            intStatus = imageStream.Read(imageContent, 0, imageSize) 
 
            Dim cmd As New OleDbCommand("insert into tblImg(img_title,img_type,img_stream) values(@img_title,@img_type,@img_stream)", myAccessConnection) 
            cmd.CommandType = CommandType.Text 
            cmd.Parameters.Add("@img_title", OleDbType.VarChar).Value = txt_imgTitle.Text 
            cmd.Parameters.Add("@img_type", OleDbType.VarChar).Value = imageType 
            cmd.Parameters.Add("img_stream", OleDbType.Binary).Value = imageContent 
            cmd.ExecuteNonQuery() 
 
            closeAccessConnection() 
        Catch ex As Exception 
            Response.Write(ex.Message) 
            closeAccessConnection() 
        End Try 
 
        Response.Redirect("default.aspx") 
    End Sub 
 
    Public Function imageURL(ByVal img_id) As String 
        Return ("retrieveImages.aspx?id=" & img_id) 
    End Function 
 
End Class
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros