1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.IO
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Dim personList As New List(Of Person)()
personList.Add(New Person() With {.Id = 1, .Name = "Item1", .Picture = ""})
personList.Add(New Person() With {.Id = 2, .Name = "Item2", .Picture = "images\Item2.jpg"})
personList.Add(New Person() With {.Id = 3, .Name = "Item3", .Picture = "images\Item3.jpg"})
PersonListView.ItemsSource = personList
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim p As Person = TryCast(PersonListView.SelectedItem, Person)
p.Picture = ""
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim p As Person = TryCast(PersonListView.SelectedItem, Person)
Dim dlg As New Microsoft.Win32.OpenFileDialog()
dlg.Title = "Select image"
dlg.Filter = "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff|" + "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff"
' Show open file dialog box
Dim result? As Boolean = dlg.ShowDialog()
' Process open file dialog box results
If result = True Then
Dim filename As String = dlg.FileName
If Path.GetExtension(filename).ToLower = ".jpg" Then
File.Copy(filename, String.Concat("images\", p.Name, ".jpg"), True)
Else
Dim bitmap As New BitmapImage()
bitmap.BeginInit()
bitmap.UriSource = New Uri(filename)
bitmap.EndInit()
Dim encoder As JpegBitmapEncoder = New JpegBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(DirectCast(bitmap, BitmapImage)))
Using filestream = New FileStream(String.Concat("images\", p.Name, ".jpg"), FileMode.Create)
encoder.Save(filestream)
End Using
End If
'
p.Picture = String.Concat("images\", p.Name, ".jpg")
End If
End Sub
End Class
Public Class checkImageURL
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value = "" Then
Return Nothing
Else
Dim fullPath As String = Path.GetFullPath(value)
Dim btm As New BitmapImage()
btm.BeginInit()
btm.UriSource = New Uri(fullPath)
btm.CacheOption = BitmapCacheOption.OnLoad
btm.EndInit()
Return btm
End If
Return Nothing
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Return Nothing
End Function
End Class
Public Class Person
Implements INotifyPropertyChanged
Public Property Id() As Int32
Get
Return m_Id
End Get
Set(value As Int32)
m_Id = value
End Set
End Property
Private m_Id As Int32
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Picture() As String
Get
Return m_Picture
End Get
Set(value As String)
m_Picture = value
RaisePropertyChanged("Picture")
End Set
End Property
Private m_Picture As String
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
'
Private Sub RaisePropertyChanged(ByVal propName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
End Sub
End Class |
Partager