📄 projectphoto.vb
字号:
Imports System.IO
Public Class ProjectPhoto
Private Const strIEFile As String = "\Program Files\Internet Explorer\Iexplore.exe"
Private blnLoaded As Boolean
Private blnLoadFromFile As Boolean = True
Private Sub bindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Me.Validate Then
Me.ProductPhotoBindingSource.EndEdit()
Me.ProductPhotoTableAdapter.Update(Me.AdventureWorksDataSet.ProductPhoto)
Else
System.Windows.Forms.MessageBox.Show(Me, "Validation errors occurred.", "Save", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If blnLoadFromFile Then
If File.Exists(Application.StartupPath + "\ProductPhoto.xml") Then
AdventureWorksDataSet.ReadXml(Application.StartupPath + "\ProductPhoto.xml", _
Data.XmlReadMode.Auto)
Else
MsgBox("ProductPhoto.xml is missing.", MsgBoxStyle.Exclamation, _
"Can't Load DataSet from File")
End If
Else
Me.ProductPhotoTableAdapter.Fill(Me.AdventureWorksDataSet.ProductPhoto)
End If
Application.DoEvents()
blnLoaded = True
btnReset.Enabled = True
bindingNavigatorSaveItem.Enabled = True
If File.Exists(strIEFile) Then
btnSaveDS.Text = "Save and Display DataSet"
Else
btnSaveDS.Text = "Save DataSet"
End If
End Sub
Private Sub rbNormal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbNormal.CheckedChanged
'Normal layout
If blnLoaded And rbNormal.Checked Then
With ProductPhotoDataGridView
Dim colImage As DataGridViewImageColumn = CType(.Columns(2), DataGridViewImageColumn)
colImage.ImageLayout = DataGridViewImageCellLayout.Normal
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
End With
End If
End Sub
Private Sub rbStretch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbStretch.CheckedChanged
'Stretch layout
If blnLoaded And rbStretch.Checked Then
With ProductPhotoDataGridView
Dim colImage As DataGridViewImageColumn = CType(.Columns(2), DataGridViewImageColumn)
colImage.ImageLayout = DataGridViewImageCellLayout.Stretch
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
End With
End If
End Sub
Private Sub rbZoom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbZoom.CheckedChanged
'Zoom layout
If blnLoaded And rbZoom.Checked Then
With ProductPhotoDataGridView
Dim colImage As DataGridViewImageColumn = CType(.Columns(2), DataGridViewImageColumn)
colImage.ImageLayout = DataGridViewImageCellLayout.Zoom
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
End With
End If
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
'Size set in column editor
With ProductPhotoDataGridView
.Columns(2).Width = 240
For Each rowGrid As DataGridViewRow In .Rows
rowGrid.Height = 149
Next
End With
End Sub
Private Sub dataNavigatorSaveItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
SaveGifFile()
End Sub
Private Sub SaveGifFile()
'Save the selected file
Dim strFile As String = Nothing
Try
With ProductPhotoDataGridView
If .CurrentCell.ColumnIndex = 2 Then
If Not frmPictureBox Is Nothing Then
frmPictureBox.Close()
End If
Dim strType As String = .CurrentCell.ValueType.ToString
'Create a Byte array from the value
Dim bytImage() As Byte = CType(.CurrentCell.Value, Byte())
'Specify the image file name
Dim intRow As Integer = .CurrentCell.RowIndex
strFile = .Rows(intRow).Cells(1).Value.ToString
'Save the image as a GIF file
Dim fsImage As New FileStream("..\" + strFile, FileMode.Create)
fsImage.Write(bytImage, 0, bytImage.Length)
fsImage.Close()
'Create a MemoryStream and assign it as the image of a PictureBox
Dim msImage As New MemoryStream(bytImage)
frmPictureBox.pbBitmap.Image = Image.FromStream(msImage)
If frmPictureBox.ShowDialog = Windows.Forms.DialogResult.Yes Then
'Replace the CurrentCell's image from the saved version, if possible
If File.Exists(Application.StartupPath + "\" + strFile) Then
'The easy was to obtain a Byte array
Dim bytReplace() As Byte = File.ReadAllBytes(Application.StartupPath + "\" + strFile)
.CurrentCell.Value = bytReplace
If AdventureWorksDataSet.HasChanges Then
AdventureWorksDataSet.AcceptChanges()
Dim strMsg As String = "File '" + strFile + _
" has replaced the image in row " + intRow.ToString + _
" cell 2 (" + Format(bytReplace.Length, "#,##0") + " bytes). " + _
vbCrLf + vbCrLf + "AcceptChanges has been applied to the DataSet."
MsgBox(strMsg, MsgBoxStyle.Information, "Image Replaced from File")
Else
Dim strMsg As String = "Unable to replace image with file '" + _
strFile + "'. DataSet does not have changes."
MsgBox(strMsg, MsgBoxStyle.Exclamation, "Image Not Replaced")
End If
End If
End If
Else
MsgBox("Please select the image to save.", MsgBoxStyle.Exclamation, _
"No Image Selected")
End If
End With
Catch exc As Exception
With ProductPhotoDataGridView
If strFile = Nothing Then
Dim intRow As Integer = .CurrentCell.RowIndex
strFile = .Rows(intRow).Cells(1).Value.ToString
End If
End With
Dim strExc As String = "File '" + strFile + "' threw the following " + _
"exception: " + exc.Message
MsgBox(strExc, MsgBoxStyle.Exclamation, "Exception with Image")
End Try
End Sub
Private Sub ProductPhotoDataGridView_Click(ByVal sender As Object, ByVal e As System.EventArgs)
frmPictureBox.Close()
End Sub
Private Sub btnSaveDS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveDS.Click
AdventureWorksDataSet.WriteXml(Application.StartupPath + "\ProductPhoto.xml", Data.XmlWriteMode.IgnoreSchema)
AdventureWorksDataSet.WriteXmlSchema(Application.StartupPath + "\ProductPhoto.xsd")
'Display the DataSet file
If File.Exists(strIEFile) And File.Exists(Application.StartupPath + "\ProductPhoto.xml") Then
Dim strFile As String = Application.StartupPath + "\ProductPhoto.xml"
Dim strShell As String = """" + strIEFile + """ " + strFile
Shell(strShell, AppWinStyle.NormalFocus)
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -