在asp.net VB.net中调整图像大小

时间:2018-06-07 21:21:56

标签: asp.net vb.net bmp

在我正在处理的应用程序中,我需要调整图片大小并将其添加到数据库中我已经实现了一些操作但是我在将内存流中保存生成的BMP文件时遇到问题,以便我的实现与现有的代码。我的图像来源也来自一个名为fu(文件上传对象)的变量我也想知道访问文件源的方法是使用fu.name还是fu.Filecontents。此外,我已经将我在实施开始时实施的内容和我的实施结束的内容包括在内,因为其他代码是由一位不在我身边的同事完成的。

以下是代码:

        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

0 个答案:

没有答案