将SQL Server中的PNG图片保存为BMP / JPG

时间:2017-04-17 01:53:28

标签: sql-server sql-server-2008 vb6 adodb

我的SQL Server中有一张PNG图片,我成功获取图像并将其保存到文件中。

rs.Open "Select pic from Table", connObj, adOpenDynamic, adLockOptimistic

If rs.RecordCount > 0 Then
    If Not IsNull(rs.Fields("pic").Value) Then
        Set mstream = New ADODB.Stream
        mstream.Type = adTypeBinary
        mstream.Open
        mstream.Write rs.Fields("pic").Value
        mstream.SaveToFile App.Path & "\MyPhoto.jpg", adSaveCreateOverWrite
    End If
End If
rs.Close

正如您所看到的,我已经将我的扩展名作为.jpg,但我认为不重要,当我使用LoadPicture()在我的vb6应用程序中使用该图像时出现Invalid Picture错误。如果我打开图像并将其重新保存为jpg或bmp,LoadPicture()可以正常工作。

那么如何将图片正确保存为bmp / jpg?

1 个答案:

答案 0 :(得分:2)

您可能会发现这两个功能很有用

Private Function LoadPictureFromBlob(baData() As Byte) As StdPicture
    With CreateObject("WIA.Vector")
        .BinaryData = baData
        Set LoadPictureFromBlob = .Picture
    End With
End Function

Private Sub SavePictureToJpg(oPic As StdPicture, sFile As String, Optional ByVal Quality = 80)
    Const wiaFormatJPEG As String = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
    Dim oImg            As Object

    SavePicture oPic, sFile
    Set oImg = CreateObject("WIA.ImageFile")
    oImg.LoadFile sFile
    With CreateObject("WIA.ImageProcess")
        .Filters.Add .FilterInfos("Convert").FilterID
        .Filters.Item(1).Properties("FormatID").Value = wiaFormatJPEG
        .Filters.Item(1).Properties("Quality").Value = Quality
        Set oImg = .Apply(oImg)
    End With
    On Error Resume Next
    Kill sFile
    On Error GoTo 0
    oImg.SaveFile sFile
End Sub

只需使用Set oPic = LoadPictureFromBlob(rs.Fields("pic").Value)阅读.png,然后使用内置SavePicture oPic, App.Path & "\MyPhoto.bmp"保存到.bmpSavePictureToJpg oPic, App.Path & "\MyPhoto.jpg" .jpg并决定可选的质量参数。