裁剪图像并使用vb.net将其保存在数据库中

时间:2016-05-08 04:00:34

标签: vb.net

我正在关注Image Cropping with resizing using vb.net 的教程。一切都运作良好,但不是保存它 在硬盘上。我想将它保存在我的数据库(SQLServer)上。

这是在磁盘上保存的代码

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cropSaveBtn.Click
        Dim tempFileName As String
        Dim svdlg As New SaveFileDialog()
        svdlg.Filter = "JPEG files (*.jpg)|*.jpg|All files (*.*)|*.*"
        svdlg.FilterIndex = 1
        svdlg.RestoreDirectory = True
        If svdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            tempFileName = svdlg.FileName           'check the file exist else save the cropped image
            Try
                Dim img As Image = PreviewPictureBox.Image

                SavePhoto(img, tempFileName, 225)
            Catch exc As Exception
                MsgBox("Error on Saving: " & exc.Message)
            End Try
        End If
    End Sub

    Public Function SavePhoto(ByVal src As Image, ByVal dest As String, ByVal w As Integer) As Boolean
        Try
            Dim imgTmp As System.Drawing.Image
            Dim imgFoto As System.Drawing.Bitmap

            imgTmp = src
            imgFoto = New System.Drawing.Bitmap(w, 225)
            Dim recDest As New Rectangle(0, 0, w, imgFoto.Height)
            Dim gphCrop As Graphics = Graphics.FromImage(imgFoto)
            gphCrop.SmoothingMode = SmoothingMode.HighQuality
            gphCrop.CompositingQuality = CompositingQuality.HighQuality
            gphCrop.InterpolationMode = InterpolationMode.High

            gphCrop.DrawImage(imgTmp, recDest, 0, 0, imgTmp.Width, imgTmp.Height, GraphicsUnit.Pixel)

            Dim myEncoder As System.Drawing.Imaging.Encoder
            Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter
            Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters

            Dim arrayICI() As System.Drawing.Imaging.ImageCodecInfo = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
            Dim jpegICI As System.Drawing.Imaging.ImageCodecInfo = Nothing
            Dim x As Integer = 0
            For x = 0 To arrayICI.Length - 1
                If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
                    jpegICI = arrayICI(x)
                    Exit For
                End If
            Next
            myEncoder = System.Drawing.Imaging.Encoder.Quality
            myEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
            myEncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, 60L)
            myEncoderParameters.Param(0) = myEncoderParameter
            imgFoto.Save(dest, jpegICI, myEncoderParameters)
            imgFoto.Dispose()
            imgTmp.Dispose()

        Catch ex As Exception

        End Try
    End Function

我希望它将图片与我的两个数据一起保存到SQL Server 2008(图像数据类型)

   Using cmd As New SqlClient.SqlCommand("dbo.uspAdd", cn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@firstname", SqlDbType.VarChar, 100).Value = txtName.Text
        cmd.Parameters.Add("@lastName", SqlDbType.VarChar, 100).Value = txtSurname.Text
        'add insert picture code here   
        cmd.ExecuteNonQuery()
        MsgBox("Save Record New record Successfully")
    End Using

现在我已经坚持了将近8个小时,想办法解决这个问题。任何人都可以帮我解决这个问题。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我建议将图像转换为Base64,然后将其存储为字符串。

然后,当您想要恢复图像时,需要将其从Base64转换回图像。

您可以使用以下代码将图像转换为Base64:

  Public Function ConvertImageToBase64(ByRef img As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
        Dim ImgStream As MemoryStream = New MemoryStream()
        img.Save(ImgStream, format)
        ImgStream.Close()
        Dim ByteArray() As Byte = ImgStream.ToArray()
        ImgStream.Dispose()
        Return Convert.ToBase64String(ByteArray)
    End Function

http://www.dailycoding.com/posts/convert_image_to_base64_string_and_base64_string_to_image.aspx

要将其转换回图像,您可以使用以下代码:

 Public Function ConvertBase64ToImage(ByVal base64 As String) As Image
        Dim img As System.Drawing.Image
        Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream
        Dim b64 As String = base64.Replace(" ", "+")
        Dim b() As Byte
        b = Convert.FromBase64String(b64)
        MS = New System.IO.MemoryStream(b)
        img = System.Drawing.Image.FromStream(MS)
        Return img
    End Function

http://snipplr.com/view/27514/vbnet-base64-to-image/

现在您基本上可以将图像添加为字符串,如下所示:

Dim Base64Bitmap As String = ConvertImageToBase64(img, System.Drawing.Imaging.ImageFormat.Png)
cmd.Parameters.Add("@Image", SqlDbType.VarChar, Base64Bitmap.Length).Value = Base64Bitmap