Avatar of tjgrindsted
 asked on

Help with watermark for uploading image to Access DB

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 
        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 
        End If 
    End Sub 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
            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)) 
                    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 
        Catch ex As Exception 
        End Try 
    End Sub 
    Public Function imageURL(ByVal img_id) As String 
        Return ("retrieveImages.aspx?id=" & img_id) 
    End Function 
End Class

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon


no one that can help me with my code..im new to this.

is there really no one that can help here !?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question