2011-10-05 3 views
0

VB 2010을 사용하여 데이터베이스 (SQL Server 2008 R2)에 이미지를 저장하는 데 사용한 코드는 다음과 같습니다. 이미지는 저장되지만 검색시 이미지의 선명도가 문제가됩니다. 그림 상자에서 본.데이터베이스에 이미지 저장 및 검색

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 
+0

을 수행해야합니다 아래 (테스트되지 않은) 코드. –

답변

1

image.save()는 이미지의 품질을 (JPEG로 저장 한 경우) 약 75 %의 기본 압축 수준으로 낮 춥니 다.

(예를 들어 90 %)

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

당신이 myEncoderParameters를 전달하여 저장을 호출 할 때,이 품질 수준을 증가 훨씬 높은 수준에서 품질 수준을 포함에이 MSDN 문서를 참조 또는 참조하십시오 트릭 당신이 이미지를 검색하고 표시하는 방법에 대한 좀 더 많은 정보를 포함 할 필요가 있다고 생각

' 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) 
관련 문제