Skip to content

Commit dcaaf00

Browse files
committed
Add an image to contact
1 parent aa4388f commit dcaaf00

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

EF Sqlite/Contacts/Contact.vb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,20 @@ Public Class Contact
1313
Property EmailAddress As String
1414
<Required>
1515
Property Birthdate As Date = Now.Date.AddYears(-18)
16+
<NotMapped>
17+
ReadOnly Property Image As Image
18+
Get
19+
If If(ImageBytes?.Length, 0) = 0 Then Return Nothing
20+
Using mem As New IO.MemoryStream(ImageBytes)
21+
Using tmp = Image.FromStream(mem)
22+
Return DirectCast(tmp.Clone(), Image)
23+
End Using
24+
End Using
25+
End Get
26+
End Property
27+
Property ImageBytes As Byte()
1628

17-
Public Overrides Function ToString As String
29+
Public Overrides Function ToString() As String
1830
Return $"ContactID: {ContactID}, Name: {Name}, EmailAddress: {EmailAddress}"
1931
End Function
2032
End Class

EF Sqlite/Form1.Designer.vb

Lines changed: 15 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EF Sqlite/Form1.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<metadata name="ImageColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
121+
<value>True</value>
122+
</metadata>
120123
<metadata name="ContactBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
121124
<value>17, 17</value>
122125
</metadata>

EF Sqlite/Form1.vb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,39 @@ Public Class Form1
4747

4848

4949

50+
End Sub
51+
Function GetImage() As (Image As Byte(), Success As Boolean)
52+
Dim bytes As Byte()
53+
Dim ofd As New OpenFileDialog With {
54+
.Title = "Select Image",
55+
.Filter = "Image files|*.jpg;*.png;*.gif;*.bmp|All files (*.*)|*.*",
56+
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
57+
}
58+
Dim dlg = ofd.ShowDialog
59+
60+
If dlg = DialogResult.OK Then
61+
Try
62+
bytes = My.Computer.FileSystem.ReadAllBytes(ofd.FileName)
63+
Using mem As New IO.MemoryStream(bytes)
64+
Using tmp As Image = Image.FromStream(mem) ' make sure it is a valid image
65+
Return (bytes, True)
66+
End Using
67+
End Using
68+
69+
Catch ex As Exception
70+
MessageBox.Show($"Could not load as an image.{ex.Message}", "Error Loading Image", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
71+
End Try
72+
ElseIf dlg = DialogResult.Cancel Then
73+
If MessageBox.Show($"Clear Image?", "Unset Image", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then Return (Nothing, True)
74+
End If
75+
Return (Nothing, False)
76+
End Function
77+
78+
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
79+
If e.ColumnIndex = ImageColumn.Index AndAlso Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
80+
Dim record As Contact = DataGridView1.Rows(e.RowIndex).DataBoundItem
81+
Dim img = GetImage()
82+
If img.Success Then record.ImageBytes = img.Image
83+
End If
5084
End Sub
5185
End Class

0 commit comments

Comments
 (0)