This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMainPage.xaml.vb
215 lines (159 loc) · 7.48 KB
/
MainPage.xaml.vb
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
'
'* Copyright (c) 2014 Microsoft Mobile
'*
'* Permission is hereby granted, free of charge, to any person obtaining a copy
'* of this software and associated documentation files (the "Software"), to deal
'* in the Software without restriction, including without limitation the rights
'* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
'* copies of the Software, and to permit persons to whom the Software is
'* furnished to do so, subject to the following conditions:
'* The above copyright notice and this permission notice shall be included in
'* all copies or substantial portions of the Software.
'*
'* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
'* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
'* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
'* THE SOFTWARE.
'
Imports Lumia.Imaging
Imports Lumia.Imaging.Adjustments
Imports Windows.Storage
Imports Windows.Storage.Pickers
Imports Windows.Storage.Streams
Imports Windows.UI.Popups
''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
Inherits Page
Private _grayscaleEffect As GrayscaleEffect
Private _brightnessEffect As BrightnessEffect
Private m_renderer As SwapChainPanelRenderer
' The following WriteableBitmap contains
' The filtered and thumbnail image.
Private _writeableBitmap As WriteableBitmap
Private _thumbnailImageBitmap As WriteableBitmap
Public Sub New()
InitializeComponent()
Dim scaleFactor As Double = 1.0
scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel
_writeableBitmap = New WriteableBitmap(CInt(Window.Current.Bounds.Width * scaleFactor), CInt(Window.Current.Bounds.Height * scaleFactor))
_thumbnailImageBitmap = New WriteableBitmap(CInt(OriginalImage.Width), CInt(OriginalImage.Height))
_grayscaleEffect = New GrayscaleEffect()
_brightnessEffect = New BrightnessEffect(_grayscaleEffect)
End Sub
Private Async Sub OnSwapChainPanelLoaded(sender As Object, e As RoutedEventArgs) Handles SwapChainPanelTarget.Loaded
If SwapChainPanelTarget.ActualHeight > 0 AndAlso SwapChainPanelTarget.ActualWidth > 0 Then
If m_renderer Is Nothing Then
m_renderer = New SwapChainPanelRenderer(_brightnessEffect, SwapChainPanelTarget)
Await LoadDefaultImageAsync()
End If
End If
End Sub
Private Async Sub OnSwapChaninPanelSizeChaged(sender As Object, e As SizeChangedEventArgs) Handles SwapChainPanelTarget.SizeChanged
If m_renderer Is Nothing Then
Return
End If
Await m_renderer.RenderAsync()
End Sub
Private Async Function LoadDefaultImageAsync() As Task
Dim file = Await StorageFile.GetFileFromApplicationUriAsync(New System.Uri("ms-appx:///Assets/defaultImage.jpg"))
Await ApplyEffectAsync(file)
End Function
Private Async Function ApplyEffectAsync(file As StorageFile) As Task(Of Boolean)
' Open a stream for the selected file.
Dim fileStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
Dim errorMessage As String = Nothing
Try
' Show thumbnail of original image.
_thumbnailImageBitmap.SetSource(fileStream)
OriginalImage.Source = _thumbnailImageBitmap
' Rewind stream to start.
fileStream.Seek(0)
' Set the imageSource on the effect and render
DirectCast(_grayscaleEffect, IImageConsumer).Source = New Lumia.Imaging.RandomAccessStreamImageSource(fileStream)
Await m_renderer.RenderAsync()
Catch exception As Exception
errorMessage = exception.Message
End Try
If Not String.IsNullOrEmpty(errorMessage) Then
Dim dialog = New MessageDialog(errorMessage)
Await dialog.ShowAsync()
Return False
End If
Return True
End Function
Private Async Function SaveImageAsync(file As StorageFile) As Task(Of Boolean)
If _grayscaleEffect Is Nothing Then
Return False
End If
Dim errorMessage As String = Nothing
Try
Using jpegRenderer = New JpegRenderer(_grayscaleEffect)
Using stream = Await file.OpenAsync(FileAccessMode.ReadWrite)
' Jpeg renderer gives the raw buffer containing the filtered image.
Dim jpegBuffer As IBuffer = Await jpegRenderer.RenderAsync()
Await stream.WriteAsync(jpegBuffer)
Await stream.FlushAsync()
End Using
End Using
Catch exception As Exception
errorMessage = exception.Message
End Try
If Not String.IsNullOrEmpty(errorMessage) Then
Dim dialog = New MessageDialog(errorMessage)
Await dialog.ShowAsync()
Return False
End If
Return True
End Function
Private Sub PickImage_Click(sender As Object, e As RoutedEventArgs)
Dim openPicker = New FileOpenPicker()
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
openPicker.ViewMode = PickerViewMode.Thumbnail
' Filter to include a sample subset of file types.
openPicker.FileTypeFilter.Clear()
openPicker.FileTypeFilter.Add(".bmp")
openPicker.FileTypeFilter.Add(".png")
openPicker.FileTypeFilter.Add(".jpeg")
openPicker.FileTypeFilter.Add(".jpg")
PickImage(openPicker)
End Sub
Private Sub SaveImage_Click(sender As Object, e As RoutedEventArgs) Handles SaveButton.Click
SaveButton.IsEnabled = False
Dim savePicker = New FileSavePicker()
savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
savePicker.SuggestedFileName = String.Format("QuickstartImage_{0}", DateTime.Now.ToString("yyyyMMddHHmmss"))
savePicker.FileTypeChoices.Add("JPG File", New List(Of String)() From {".jpg"})
SaveImage(savePicker)
SaveButton.IsEnabled = False
End Sub
Private Async Sub PickImage(openPicker As FileOpenPicker)
' Open the file picker.
Dim file As StorageFile = Await openPicker.PickSingleFileAsync()
' file is null if user cancels the file picker.
If file IsNot Nothing Then
If Not (Await ApplyEffectAsync(file)) Then
Return
End If
SaveButton.IsEnabled = True
End If
End Sub
Private Async Sub SaveImage(savePicker As FileSavePicker)
Dim file = Await savePicker.PickSaveFileAsync()
If file IsNot Nothing Then
Await SaveImageAsync(file)
End If
SaveButton.IsEnabled = True
End Sub
Private Async Sub OnBrightnessChanged(sender As Object, e As RangeBaseValueChangedEventArgs)
_brightnessEffect.Level = e.NewValue
If DirectCast(_grayscaleEffect, IImageConsumer).Source IsNot Nothing Then
Await m_renderer.RenderAsync()
End If
End Sub
End Class