📄 categories.vb
字号:
Option Explicit On
Option Strict On
Imports System.IO
Public Class Categories
Private blnLoaded As Boolean
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: Delete this line of code to remove the default AutoFill for 'NorthwindDataSet.Categories'.
Me.CategoriesTableAdapter.Fill(Me.NorthwindDataSet.Categories)
Application.DoEvents()
blnLoaded = True
btnReset.Enabled = True
End Sub
Private Sub rbNormal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbNormal.CheckedChanged
If blnLoaded And rbNormal.Checked Then
With CategoriesDataGridView
Dim colImage As DataGridViewImageColumn = CType(.Columns(3), DataGridViewImageColumn)
colImage.ImageLayout = DataGridViewImageCellLayout.Normal
End With
End If
End Sub
Private Sub rbStretch_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbStretch.CheckedChanged
If blnLoaded And rbStretch.Checked Then
With CategoriesDataGridView
Dim colImage As DataGridViewImageColumn = CType(.Columns(3), DataGridViewImageColumn)
colImage.ImageLayout = DataGridViewImageCellLayout.Stretch
End With
End If
End Sub
Private Sub rbZoom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbZoom.CheckedChanged
If blnLoaded And rbZoom.Checked Then
With CategoriesDataGridView
Dim colImage As DataGridViewImageColumn = CType(.Columns(3), DataGridViewImageColumn)
colImage.ImageLayout = DataGridViewImageCellLayout.Zoom
End With
End If
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
With CategoriesDataGridView
.Columns(3).Width = 100
For Each rowGrid As DataGridViewRow In .Rows
rowGrid.Height = 75
Next
End With
End Sub
Private Sub btnTestSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestSave.Click
SaveBmpFile()
End Sub
Private Sub SaveBmpFile()
'Save the selected file
Dim strFile As String = Nothing
Try
With CategoriesDataGridView
If .CurrentCell.ColumnIndex = 3 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 + ".bmp"
'Save the image as a GIF file
Dim fsImage As New FileStream(Application.StartupPath + "\" + 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("..\" + strFile) Then
'The easy was to obtain a Byte array
Dim bytReplace() As Byte = File.ReadAllBytes(Application.StartupPath + "\" + strFile)
.CurrentCell.Value = bytReplace
If NorthwindDataSet.HasChanges Then
NorthwindDataSet.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 CategoriesDataGridView
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
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -