【问题标题】:Crop image and save it in database using vb.net使用 vb.net 裁剪图像并将其保存在数据库中
【发布时间】:2016-05-08 04:00:34
【问题描述】:

我正在关注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 个小时,想方设法解决这个问题。谁能帮我解决这个问题。任何帮助将不胜感激。

【问题讨论】:

  • 也许这有帮助; stackoverflow.com/questions/16842065/… 或者您可以转换为 base64 并返回...?
  • @Lectere 你能帮我在我的代码中使用它吗?
  • 首先,考虑不是保存图像数据,而是图像文件的路径。image data type was deprecated long ago,因此您应该使用 VarBinary 而不是使用字节数组。不要保存为 B64 字符串 - 这将比需要的大得多。也使用空的 catch 块是一个非常糟糕的主意
  • @Plutonix,谢谢你给我这个想法,你能帮我建立一个符合我的代码的代码,以便我继续前进。
  • this answer 显示了一般步骤,但正如我所说,通常最好将文件保存在某处并仅引用它。您可以避免必须缩略图(== 裁剪???)图像并丢弃数据。

标签: vb.net


【解决方案1】:

我建议将图像转换为 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

【讨论】:

  • ,感谢您的回答。当我尝试上面的代码时,我在Dim Base64Bitmap As String = ConvertImageToBase64(img, System.Drawing.Imaging.ImageFormat.Png) 这一行有一个错误,说'img' is not declared. It may be inaccessible due to its protection level.
  • 您是否将图像声明为“img”,就像我在您的示例中看到的一样,您将图像对象声明为“src”或“imgTmp”。尝试使用“src”或代替 img。
猜你喜欢
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多