VB 图片在数据库的导入与导出

 1 Private Sub fuck()
 2         Try
 3             Dim o_strpath, o_strname, o_houzui As String
 4             Dim o_intNPost As Integer, o_intSPost As Integer, o_intLen As Integer
 5             Dim o_intNlast As Integer
 6             Dim o_bytBit() As Byte
 7             o_strpath = "C:\Documents and Settings\vinsonlu\My Documents\My Pictures\logo\003.jpg"
 8             If File.Exists(o_strpath) Then
 9                 o_intNPost = o_strpath.LastIndexOf("\")
10                 o_intSPost = o_strpath.LastIndexOf(".")
11                 o_intLen = o_intSPost - o_intNPost
12                 o_strname = o_strpath.Substring(o_intNPost + 1, o_intLen - 1)
13                 o_houzui = o_strpath.Substring(o_intSPost + 1)
14                 o_strname = o_strname + "." + o_houzui
15                 txtRemark1.Text = o_strpath
16                 Dim fs As FileStream
17                 Dim br As BinaryReader
18                 fs = New FileStream(o_strpath, FileMode.Open, FileAccess.Read)
19                 br = New BinaryReader(fs)
20                 o_bytBit = br.ReadBytes(fs.Length)
21 
22                 Dim sqlcmd_shangchuan As New SqlClient.SqlCommand
23                 Dim o_scCon As New SqlConnection
24                 Try
25                     o_scCon.ConnectionString = connectstrinG()
26                     o_scCon.Open()
27 
28                 Catch ex As Exception
29                     System.Console.WriteLine(ex.ToString)
30                     MsgBox("cannot connect to SQL Server !", MsgBoxStyle.Critical, "Connection Error")
31                     Return
32                 End Try
33                 With sqlcmd_shangchuan
34                     .Connection = o_scCon
35                     .CommandType = CommandType.StoredProcedure
36                     .CommandText = "fuck"
37                     .Parameters.Clear()
38                     .Parameters.Add("@image", SqlDbType.Image, o_bytBit.Length).Value = o_bytBit
39                     .ExecuteNonQuery()
40                 End With
41 
42             End If
43         Catch ex As Exception
44         End Try
45     End Sub

存儲過程:

 1 set ANSI_NULLS ON
 2 set QUOTED_IDENTIFIER ON
 3 go
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 ALTER    proc [dbo].[fuck]
14 @image image
15 as
16 
17 
18 update CompanyProfile set CompanyLogo=@image where CompanyCode='Brunet_pk'

按指定长宽导出到硬盘:

 1 Private Function getImage(ByVal CompanyCode As String, ByVal x As Short, ByVal y As Short) As Boolean
 2         Try
 3             Dim command As New SqlCommand("select CompanyLogo from CompanyProfile where CompanyCode='" + CompanyCode + "'", SqlConnect)
 4             Dim by As Byte() = DirectCast(command.ExecuteScalar(), Byte())
 5             If Not System.IO.Directory.Exists("C:\TEMP\") Then
 6                 System.IO.Directory.CreateDirectory("C:\TEMP\")
 7             End If
 8             Dim LogoFileName As String = "C:\TEMP\BILogo.jpg"
 9             Dim TmpLogo As Bitmap = gData.ChangeImageSize(by, x, y)
10             TmpLogo.Save(LogoFileName)
11             command.Dispose()
12             Return True
13         Catch ex As Exception
14             Return False
15         End Try
16     End Function

在winform界面显示:

 1     Private Sub Filldgv_image()
 2         '列表顯示
 3         Try
 4             Dim datatable As DataTable = gData.GetDataTable("select featmgid,featmgnam,cretuser,convert(varchar(10),cretdate,21 ) as cretdate from cd_featureimages " + strwhere_image + " order by ledtdate desc ", SqlConnect)
 5 
 6             With dgv_image
 7                 .DataSource = datatable
 8                 .Columns("featmgid").Caption = "Image ID"
 9                 .Columns("featmgnam").Caption = "Image Name"
10                 .Columns("cretuser").Caption = "Creation User"
11                 .Columns("cretdate").Caption = "Creation Date"
12                 For I As Int16 = 0 To .Columns.Count - 1
13                     .Splits(0).DisplayColumns(I).AutoSize()
14                 Next
15             End With
16         Catch ex As Exception
17             MessageBox.Show(ex.ToString())
18         End Try
19 
20         '圖片顯示
21         'picture1
22         Try
23             Dim command As New SqlCommand("select image from cd_featureimages " + strwhere_image + " and featmgid=1", SqlConnect)
24             Dim by As Byte() = DirectCast(command.ExecuteScalar(), Byte())
25             With PictureBox1
26                 .Controls.Clear()
27                 .Image = LoadImage(by)
28             End With
29             command.Dispose()
30         Catch ex As Exception
31 
32         End Try
33 
34         'picture2
35         Try
36             Dim command As New SqlCommand("select image from cd_featureimages " + strwhere_image + " and featmgid=2", SqlConnect)
37             Dim by As Byte() = DirectCast(command.ExecuteScalar(), Byte())
38             With PictureBox2
39                 .Controls.Clear()
40                 .Image = LoadImage(by)
41             End With
42             command.Dispose()
43         Catch ex As Exception
44 
45         End Try
46     End Sub
47 
48 Private Function LoadImage(ByVal by As Byte()) As System.Drawing.Bitmap
49         Try
50             If by Is Nothing Then
51                 Return Nothing
52             End If
53             Me.Cursor = Cursors.WaitCursor
54             Dim m_PicW As Integer = 168
55             Dim m_PicH As Integer = 140
56             Dim ms As New IO.MemoryStream(by)
57             Dim imgT As New PictureBox
58             imgT.SizeMode = PictureBoxSizeMode.AutoSize
59             imgT.Image = Image.FromStream(ms)
60 
61             Dim bmp As New System.Drawing.Bitmap(m_PicW, m_PicH)
62             Dim grp As Graphics = Graphics.FromImage(bmp)
63             Dim blueBrush As New SolidBrush(Color.White)
64 
65             grp.FillRectangle(blueBrush, 0, 0, m_PicW, m_PicH)
66             Dim intW As Single
67             Dim intH As Single
68             If imgT.Width > m_PicW Then
69                 intW = m_PicW
70                 intH = imgT.Height * (m_PicW / imgT.Width)
71             Else
72                 intW = imgT.Width
73                 intH = imgT.Height
74             End If
75             If intH > m_PicH Then
76                 intH = m_PicH
77                 intW = imgT.Width * (m_PicH / imgT.Height)
78             End If
79 
80             grp.DrawImage(imgT.Image, (m_PicW - intW) / 2, (m_PicH - intH) / 2, intW, intH)
81 
82             ms.Close()
83             Me.Cursor = Cursors.Default
84             Return bmp
85         Catch ex As Exception
86             Me.Cursor = Cursors.Default
87             MsgBox(ex.ToString, MsgBoxStyle.Critical, CD_SYSTEM_NAME)
88             Return Nothing
89         End Try
90     End Function
vinson
原文地址:https://www.cnblogs.com/vinsonLu/p/3025685.html