在数据库中存储和检索图像 [英] Storing and retrieving images in Database

查看:139
本文介绍了在数据库中存储和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我使用VB 2010在我的数据库(SQL Server 2008 R2)中存储图像的代码。
图像存储但问题是图像的清晰度在检索和查看时丢失一个图片框。

Here is the code I used to store an image in my database (SQL Server 2008 R2) using VB 2010. The images get stored but the problem is the clarity of the image is lost when retrieved and seen in a picture box.

Public Function InsertUpdateImage(ByRef _SqlConnection As System.Data.SqlClient.SqlConnection, ByVal _Image As System.Drawing.Image, ByVal _ImageFormat As System.Drawing.Imaging.ImageFormat) As Integer
    Dim _SqlRetVal As Integer = 0
    'System.IO.Path.GetFullPath(files(ListView1.SelectedIndices(0))) Give the path for the 'image from listview 
    Dim str As String = System.IO.Path.GetFullPath(files(ListView1.SelectedIndices(0)))
    Dim i As Integer = Len(str)
    Dim j As Integer = 0
    Dim locstr(i + 10) As Char
    i = 0
    While i < Len(str)
        If str(i) = "\" Then
            locstr(j) = "\"
            j = j + 1
        Else
            locstr(j) = str(i)
            j = j + 1
        End If
        i = i + 1
    End While
    Dim loc As New String(locstr)
    MsgBox(loc)

    ' lets add this record to database
    Dim _SqlCommand As New System.Data.SqlClient.SqlCommand("insert into maindb(photo,name,location) values(@image,'" + System.IO.Path.GetFileName(files(ListView1.SelectedIndices(0))) + "','" + loc + "')", _SqlConnection)

    ' Convert image to memory stream
    Dim _MemoryStream As New System.IO.MemoryStream()
    _Image.Save(_MemoryStream, _ImageFormat)

    ' Add image as SQL parameter
    Dim _SqlParameter As New System.Data.SqlClient.SqlParameter("@image", SqlDbType.Image)
    _SqlParameter.Value = _MemoryStream.ToArray()

    _SqlCommand.Parameters.Add(_SqlParameter)

    ' Executes a Transact-SQL statement against the connection 
    ' and returns the number of rows affected.
    _SqlRetVal = _SqlCommand.ExecuteNonQuery()
    Console.Write(_SqlRetVal)
    ' Dispose command
    _SqlCommand.Dispose()
    _SqlCommand = Nothing

    ' Error occurred while trying to execute reader
    ' send error message to console (change below line to customize error handling)

    Return _SqlRetVal
End Function


推荐答案

您的image.save()会降低图像的质量(如果保存为Jpeg)到默认压缩级别大约75%。

Your image.save() reduces the quality of the image (if saved as Jpeg) to the default compression level of aabout 75%.

当你通过传入包含质量的myEncoderParameters来调用Save时,请参阅这篇关于提高质量等级的MSDN文章等级更高(比如90%)

Please see this MSDN article on increasing this quality level, when you call Save by passing in myEncoderParameters, containing a quality level at a much higher level (say 90%)

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.quality.aspx

O. r看下面的(未经测试的)代码,这应该做的诀窍

Or see the (untested) code below, that should do the trick

    ' Create a a single encoder parameter envelope
    Dim EncoderParameters As New EncoderParameters(1)

    ' Create and add a single quality parameter to this envelope, specifying 95%
    Dim QualityParam As New EncoderParameter(Encoder.Quality, CType(95L, Int32))
    EncoderParameters.Param(0) = QualityParam

    ' Save the image with the encoder param specifying 95% quality
    _image.Save(_MemoryStream, _ImageFormat, EncoderParameters)

这篇关于在数据库中存储和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆