0

In the application I am working on I need to resize a picture and add it to the database I have implemented something to do so but I have a problem saving the resulting BMP file in the memory stream so that my implementation works with the existing code.Also the source from my image comes from a variable called fu (File upload object) I was also wondering if the way to access the source of the file is to use fu.name or fu.Filecontents. Also I have Enclosed what I implemented in beginning of my implementation and Ending of my implementation as the other code was done by a colleague who is not with me anymore.

Here is the code:

        Public Function UploadFile(fu As FileUpload, expID As Integer) As Integer
    GetConnectionString()
    Dim con As New SqlConnection(connString.ConnectionString)
    Dim dataAdapter As New SqlDataAdapter
    Dim myCommandBuilder As SqlCommandBuilder
    Dim dataSet As New DataSet
    Dim memoryStream As MemoryStream
    Dim bData As Byte()
    Dim reader As BinaryReader
    Dim expense As New Expense(expID)
    Dim loggedInUser As New Employee(Membership.GetUser.UserName)

    Try
        UploadFile = 1
        'check that the logged in user has the right to attach a file to the current expense
        If loggedInUser.ID = expense.Rpt.Emp.ID Or loggedInUser.ID = expense.Rpt.Emp.Supervisor Or loggedInUser.ID = expense.Rpt.Emp.Finalizer Or loggedInUser.ID = expense.Rpt.Emp.DelegatedTo Then
            If fu.HasFile Then
                If fu.FileContent.Length < 5000000 Then
                    dataAdapter = New SqlDataAdapter("SELECT * FROM tblExpense WHERE EXPENSE_ID=" & expID, con)
                    myCommandBuilder = New SqlCommandBuilder(dataAdapter)
                    dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
                    con.Open()
                    dataAdapter.Fill(dataSet, "tblExpense")
                    'Begining of my implementation
                    ' Get the scale factor.
                    Dim scale_factor As Single = Single.Parse(0.33)
                    ' Get the source bitmap.
                    Dim bm_source As New Bitmap(fu.FileContent)
                    ' Make a bitmap for the result.
                    Dim bm_dest As New Bitmap(
                    CInt(bm_source.Width * scale_factor),
                    CInt(bm_source.Height * scale_factor))
                    ' Make a Graphics object for the result Bitmap.
                    Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
                    ' Copy the source image into the destination bitmap.
                    gr_dest.DrawImage(bm_source, 0, 0,
                    bm_dest.Width + 1,
                    bm_dest.Height + 1)

                    'Ending of my implementation
                    reader = New BinaryReader(fu.FileContent)
                    bData = reader.ReadBytes(reader.BaseStream.Length)
                    memoryStream = New MemoryStream(bData, 0, bData.Length)
                    memoryStream.Close()

                    dataSet.Tables("tblExpense").Rows(0)("RECEIPT") = bData

                    Select Case UCase(Right(fu.PostedFile.FileName, 3))
                        Case "JPG" : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "image/jpeg"
                        Case "PNG" : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "image/png"
                        Case "GIF" : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "image/gif"
                        Case "PDF" : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "application/pdf"
                        Case "TXT" : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "text/plain"
                        Case "HTM" : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "text/html"
                        Case "HTML" : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "text/html"
                        Case Else : dataSet.Tables("tblExpense").Rows(0)("RECEIPT_TYPE") = "image/jpeg"
                    End Select
                    dataSet.Tables("tblExpense").Rows(0)("RECEIPT_NAME") = expense.Rpt.Emp.EmpNum & "-" & expense.Rpt.Name & "-" & expense.ID
                    dataSet.Tables("tblExpense").Rows(0)("RECEIPT_DATE") = Now
                    dataAdapter.Update(dataSet, "tblExpense")

                Else
                    UploadFile = 2
Omar
  • 1
  • 2
  • yes, file upload (fu.FileContents) is the byte array. consider this [SO answer](https://stackoverflow.com/questions/2319983/resizing-an-image-in-asp-net-without-losing-the-image-quality) regarding image resizing. Also, don't perform any database operations until the image work is complete and correct. – fnostro Jun 07 '18 at 21:39
  • heads up, your case statement towards the bottom is a rather naive way to address file types....you'd be better off trying to look at file headers to determine what you're dealing with. – user2366842 Jun 07 '18 at 21:39
  • Possible duplicate of [Resizing an image in asp.net without losing the image quality](https://stackoverflow.com/questions/2319983/resizing-an-image-in-asp-net-without-losing-the-image-quality) – VDWWD Jun 08 '18 at 10:47

0 Answers0