下面是一段图片存储到数据库的代码
主界面
代码说明:
ItemImgClass:是一个封装的对图片信息记录的操作,如更新、插入删除记录,获取所有记录。
ItemImgDataModel:是一个封装的图片信息记录的数据模块。
添加记录:用存储过程-有插入理图片数据。但默认值为null
更新记录:用存储过程-没有处理图片数据。
图片的常规操作:选择图片,插入图片,更新图片。删除图片。
Imports System.IO
Public Class FrmItemImg
Dim OPCls As New ItemImgClass
Dim DM As ItemImgDataModel
Dim ImgPathMain, ImgPathSnd As String
Dim fs As FileStream
Private Sub LoadDataTB()
Dim tb As New DataTable
tb = OPCls.GetTableCN()
Me.Dv.DataSource = tb.DefaultView
End Sub
Private Sub ShowMsg()
Me.ShowMsg("")
End Sub
Private Sub ShowMsg(ByVal msg As String)
Me.LB.Text = String.Format("消息:{0}", msg)
End Sub
Function BoxDM(ByVal iType As Integer) As ItemImgDataModel
Dim dm As New ItemImgDataModel
dm.ItemNo = Me.TxtBoxItemNo.Text
dm.Years = Me.CmbYears.Text
dm.BigType = Me.CmbBigType.Text
dm.SmlType = Me.CmbSmlType.Text
dm.DescripEN = Me.TxtBoxDescripEN.Text
dm.DescripCN = Me.TxtBoxDescripCN.Text
dm.UpdateTime = Me.DTPImgUpdateTime.Text
dm.ImgFileName = Me.TxtBoxImgFileName.Text
dm.ImgFileName2nd = Me.TxtBoxImgFileName2nd.Text
'添加记录:存储过程-有插入理图片数据。但默认值为null
'更新记录:存储过程-没有处理图片数据。
dm.ImgBinary = Nothing
dm.ImgBinary2nd = Nothing
Select Case iType
Case 1
Case 2
'If ImgPathMain > "" Then
' 'dm.ImgBinary = getImgBinary(1)
' fs = New FileStream(ImgPathMain, FileMode.Open)
' Dim bt(fs.Length) As Byte
' fs.Read(bt, 0, fs.Length)
' dm.ImgBinary = bt
' fs.Close()
'End If
'If ImgPathSnd = "" Then
' 'dm.ImgBinary2nd = getImgBinary(2)
' fs = New FileStream(ImgPathSnd, FileMode.Open)
' Dim bt(fs.Length) As Byte
' fs.Read(bt, 0, fs.Length)
' dm.ImgBinary = bt
' fs.Close()
'End If
End Select
dm.Remarks = Me.TxtBoxRemarks.Text
Return dm
End Function
Private Function ChkSaveData() As Boolean
If Me.TxtBoxItemNo.Text = "" Then
ShowMsg("输入图片的货号!")
Return False
End If
If Me.PicBoxMain.Image Is Nothing And Me.PicBoxSnd.Image Is Nothing Then
If MsgBox("没有选择要保存的图片,是否继续保存记录。", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "保存图片记录") = MsgBoxResult.No Then
Return False
End If
End If
Return True
End Function
Private Sub BtAddNewRs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAddNewRs.Click
Me.TxtBoxItemNo.Text = ""
Me.CmbYears.Text = ""
Me.CmbBigType.Text = ""
Me.CmbSmlType.Text = ""
Me.TxtBoxDescripEN.Text = ""
Me.TxtBoxDescripCN.Text = ""
Me.DTPImgUpdateTime.Value = Now.Date()
Me.TxtBoxRemarks.Text = ""
Me.PicBoxMain.Image = Nothing
Me.PicBoxSnd.Image = Nothing
Me.TxtBoxImgFileName.Text = ""
Me.TxtBoxImgFileName2nd.Text = ""
ClearImgPathNull()
End Sub
Private Sub BindCmbBoxTB()
Dim TbYear, TbBigType, TbSmlType As New DataTable
Dim YearCls As New YearClass
Dim ImgTypeCls As New ImgTypeClass
TbYear = YearCls.GetTable()
TbBigType = ImgTypeCls.GetBigTypeTB()
TbSmlType = ImgTypeCls.GetSmlTypeTB()
Me.CmbYears.DataSource = TbYear.DefaultView
Me.CmbYears.DisplayMember = "Years"
Me.CmbYears.ValueMember = "Years"
Me.CmbYears.SelectedIndex = -1
YearCls = Nothing
Me.CmbBigType.DataSource = TbBigType.DefaultView
Me.CmbBigType.DisplayMember = "Types"
Me.CmbBigType.ValueMember = "Types"
Me.CmbBigType.SelectedIndex = -1
Me.CmbSmlType.DataSource = TbSmlType.DefaultView
Me.CmbSmlType.DisplayMember = "Types"
Me.CmbSmlType.ValueMember = "Types"
Me.CmbSmlType.SelectedIndex = -1
ImgTypeCls = Nothing
End Sub
Private Sub ClearImgPathNull()
ImgPathMain = ""
ImgPathSnd = ""
End Sub
Private Sub BtSaveRs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSaveRs.Click
If ChkSaveData() = False Then Exit Sub
Dim dm As ItemImgDataModel
If OPCls.CheckRsExist(Me.TxtBoxItemNo.Text) Then
'更新,不更新图片内容。
dm = BoxDM(1)
If OPCls.AddAndUpdateImg(dm) Then
OPCls.UpdateImg(dm.ItemNo, ImgPathMain, ImgPathSnd)
ClearImgPathNull()
ShowMsg("添加或更新图片信息成功。")
LoadDataTB()
Else
ClearImgPathNull()
ShowMsg("添加或更新图片信息失败。")
End If
Else
'插入,插入图片内容。
dm = BoxDM(2)
If OPCls.AddAndUpdateImg(dm) Then
OPCls.UpdateImg(dm.ItemNo, ImgPathMain, ImgPathSnd)
ClearImgPathNull()
ShowMsg("添加或更新图片信息成功。")
'RefreshDvRs()
Else
ShowMsg("添加或更新图片信息失败。")
ClearImgPathNull()
End If
End If
End Sub
Private Sub FrmItemImg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadDataTB()
BindCmbBoxTB()
End Sub
Private Sub BtDeleteRs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDeleteRs.Click
If Me.TxtBoxItemNo.Text > "" Then
If OPCls.Delete(Me.TxtBoxItemNo.Text) Then
ShowMsg("图片信息数据删除成功。")
Else
ShowMsg("图片信息数据删除失败。")
End If
End If
End Sub
Private Sub BtClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtClose.Click
Me.Close()
End Sub
Private Sub Dv_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Dv.CellClick
If e.RowIndex > -1 Then
Dim dm As New ItemImgDataModel
dm = OPCls.GetItemImgDM(Dv.Rows(e.RowIndex).Cells(0).Value)
Me.TxtBoxItemNo.Text = Dv.Rows(e.RowIndex).Cells(0).Value
Me.CmbYears.Text = Dv.Rows(e.RowIndex).Cells(1).Value
Me.CmbBigType.Text = Dv.Rows(e.RowIndex).Cells(2).Value
Me.CmbSmlType.Text = Dv.Rows(e.RowIndex).Cells(3).Value
Me.TxtBoxDescripEN.Text = Dv.Rows(e.RowIndex).Cells(4).Value
Me.TxtBoxDescripCN.Text = Dv.Rows(e.RowIndex).Cells(5).Value
Me.DTPImgUpdateTime.Value = Dv.Rows(e.RowIndex).Cells(6).Value
Me.TxtBoxImgFileName.Text = Dv.Rows(e.RowIndex).Cells(7).Value
Me.TxtBoxImgFileName2nd.Text = Dv.Rows(e.RowIndex).Cells(8).Value
Me.TxtBoxRemarks.Text = Dv.Rows(e.RowIndex).Cells(9).Value
'--加载相应的图片。
If dm.ImgBinary IsNot Nothing Then
Dim ms As New MemoryStream(dm.ImgBinary)
Me.PicBoxMain.Image = Image.FromStream(ms)
ms.Close()
ms.Dispose()
Else
Me.PicBoxMain.Image = Nothing
End If
If dm.ImgBinary2nd IsNot Nothing Then
Dim ms As New MemoryStream(dm.ImgBinary2nd)
Me.PicBoxSnd.Image = Image.FromStream(ms)
ms.Close()
ms.Dispose()
Else
Me.PicBoxSnd.Image = Nothing
End If
End If
End Sub
Private Sub TSMIPikNewImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIPikNewImg.Click
Dim p As String = ""
p = ChooseAFile("选择一个图片", "图片文件|*.jpg;*.gif;*.png;*.bmp")
If Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxMain.Name Then
ImgPathMain = ""
If p > "" Then
ImgPathMain = p
Me.TxtBoxImgFileName.Text = IO.Path.GetFileName(ImgPathMain).ToLower
Dim fs As FileStream = New FileStream(ImgPathMain, FileMode.Open)
Me.PicBoxMain.Image = Image.FromStream(fs)
fs.Close()
fs.Dispose()
Else
ShowMsg("图片路径或图片文件无效。")
End If
ElseIf Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxSnd.Name Then
ImgPathSnd = ""
If p > "" Then
ImgPathSnd = p
Me.TxtBoxImgFileName2nd.Text = IO.Path.GetFileName(ImgPathSnd).ToLower
Dim fs As FileStream = New FileStream(ImgPathSnd, FileMode.Open)
Me.PicBoxSnd.Image = Image.FromStream(fs)
fs.Close()
fs.Dispose()
Else
ShowMsg("图片路径或图片文件无效。")
End If
End If
End Sub
Private Sub ShowMaxImg(ByVal iType As Integer)
Select Case iType
Case 1
If ImgPathMain > "" Then
Dim mfrm As New FrmShowMaxImg(ImgPathMain)
mfrm.ShowDialog()
Else
If Me.PicBoxMain.Image IsNot Nothing Then
Dim mfrm As New FrmShowMaxImg(Me.PicBoxMain.Image)
mfrm.ShowDialog()
Else
ShowMsg("没有图片数据,无法显示。")
End If
End If
Case 2
If ImgPathSnd > "" Then
Dim mfrm As New FrmShowMaxImg(ImgPathSnd)
mfrm.ShowDialog()
Else
If Me.PicBoxSnd.Image IsNot Nothing Then
Dim mfrm As New FrmShowMaxImg(Me.PicBoxSnd.Image)
mfrm.ShowDialog()
Else
ShowMsg("没有图片数据,无法显示。")
End If
End If
End Select
End Sub
Private Sub TSMIShowMaxImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIShowMaxImg.Click
If Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxMain.Name Then
ShowMaxImg(1)
ElseIf Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxSnd.Name Then
ShowMaxImg(2)
End If
End Sub
Private Sub CleanImgBox(ByVal iType As Integer)
Select Case iType
Case 1
ImgPathMain = ""
Me.PicBoxMain.Image = Nothing
Case 2
ImgPathSnd = ""
Me.PicBoxSnd.Image = Nothing
Case 3
ImgPathSnd = ""
Me.PicBoxSnd.Image = Nothing
ImgPathMain = ""
Me.PicBoxMain.Image = Nothing
End Select
End Sub
Private Sub TSMICleanImgBoxData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMICleanImgBoxData.Click
If Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxMain.Name Then
CleanImgBox(1)
ElseIf Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxSnd.Name Then
CleanImgBox(2)
End If
End Sub
Private Sub UpdateImgBoxBin(ByVal iType As Integer)
If Me.TxtBoxItemNo.Text > "" Then
Select Case iType
Case 1
If ImgPathMain > "" Then
OPCls.UpdateImg(Me.TxtBoxItemNo.Text, ImgPathMain)
ShowMsg("更新图片成功。")
End If
Case 2
If ImgPathSnd > "" Then
OPCls.UpdateImg(Me.TxtBoxItemNo.Text, , ImgPathSnd)
ShowMsg("更新图片成功。")
End If
Case 3
If ImgPathMain > "" Then
OPCls.UpdateImg(Me.TxtBoxItemNo.Text, ImgPathMain)
End If
If ImgPathSnd > "" Then
OPCls.UpdateImg(Me.TxtBoxItemNo.Text, , ImgPathSnd)
End If
ShowMsg("更新图片成功。")
End Select
End If
End Sub
Private Sub TSMIUpdateImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIUpdateImg.Click
If Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxMain.Name Then
UpdateImgBoxBin(1)
ElseIf Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxSnd.Name Then
UpdateImgBoxBin(2)
End If
End Sub
Private Sub TSMISaveImgToDisk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMISaveImgToDisk.Click
Dim p As String = ""
Dim filter As String = "PNG图片格式|*.png|JPG图片格式|*.jpg|GIF图片格式|*.gif|BMP位图格式|*.bmp"
If Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxMain.Name Then
If Me.PicBoxMain.Image IsNot Nothing Then
p = SaveAFile("保存图片文件", filter)
If p > "" Then
Me.PicBoxMain.Image.Save(p)
ShowMsg("保存图片成功!" & p)
End If
Else
ShowMsg("没有图片数据。")
End If
ElseIf Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxSnd.Name Then
If Me.PicBoxSnd.Image IsNot Nothing Then
p = SaveAFile("保存图片文件", filter)
If p > "" Then
Me.PicBoxSnd.Image.Save(p)
ShowMsg("保存图片成功!" & p)
End If
Else
ShowMsg("没有图片数据。")
End If
End If
End Sub
Private Sub DeleteImgBoxBin(ByVal iType As Integer)
If Me.TxtBoxItemNo.Text > "" Then
Select Case iType
Case 1
OPCls.DeleteImg(Me.TxtBoxItemNo.Text, DeleteImgFlag.MainImg)
Case 2
OPCls.DeleteImg(Me.TxtBoxItemNo.Text, DeleteImgFlag.SndImg)
Case 3
OPCls.DeleteImg(Me.TxtBoxItemNo.Text, DeleteImgFlag.AllImg)
End Select
End If
End Sub
Private Sub TSMIDeleteImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSMIDeleteImg.Click
If Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxMain.Name Then
DeleteImgBoxBin(1)
ElseIf Me.CMenuForPicBox.SourceControl.Name.ToString = Me.PicBoxSnd.Name Then
DeleteImgBoxBin(2)
End If
End Sub
Private Sub TSMI2ImgOpCleanS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TSMI2ImgOpCleanS.Click
CleanImgBox(3)
End Sub
Private Sub TSMI2ImgOpUpdateS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TSMI2ImgOpUpdateS.Click
UpdateImgBoxBin(3)
End Sub
Private Sub TSMI2ImgOpDeleteS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TSMI2ImgOpDeleteS.Click
DeleteImgBoxBin(3)
End Sub
Private Sub PicBoxMain_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicBoxMain.DoubleClick
ShowMaxImg(1)
End Sub
Private Sub PicBoxSnd_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PicBoxSnd.DoubleClick
ShowMaxImg(2)
End Sub
End Class