Public Function 读取图片(id) As Image
If IsDBNull(id) OrElse id = "" Then
Return Nothing
End If
Dim cmds As New SqlCommand($"select img from 图片表 where id='{id}'", sql)
Dim dr = cmds.ExecuteReader
If dr.Read = False Then
dr.Close()
Return Nothing
End If
Dim imagebytes As Byte() = dr.GetValue(0)
dr.Close()
cmds.Dispose()
Dim MS As MemoryStream = New MemoryStream(imagebytes)
Return New Bitmap(MS)
End Function
Public Function 上传图片(bmp As Image) As String
Try
Dim id = Guid.NewGuid.ToString
Dim t = New SqlCommand($"insert into 图片表 values(N'{id}',@ImageList)", sql)
t.Parameters.Add("ImageList", SqlDbType.Image)
Dim ms As MemoryStream = New MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
t.Parameters("ImageList").Value = ms.GetBuffer
t.ExecuteNonQuery()
ms.Close()
Return id
Catch ex As Exception
Return ""
End Try
End Function
CREATE TABLE [dbo].[图片表] (
[Id] NVARCHAR(50) Not NULL,
[img] IMAGE NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);