It should be mentioned that the WritableBitmap seems to be a conundrum when examined on its face in terms of loading from a storage file. There seems to be no way to get the height and width from the file to initialize the WritableBitmap to the proper size, yet the only way to initialize the bitmap is to enter height and width parameters.
But, any time you set the source of a live WritableBitmap, it resizes accordingly. You can initialize it to any size, and then set the source, and the size, PixelWidth, and PixelHeight fields of WritableBitmap will change.
How to run this thing when done: Click on the "Get Bitmap" button to get the bitmap and see it. Then click on the Brightness box to change the brightness.
Here's page code:
<Page
x:Class="WritableBitmapExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WritableBitmapExample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" RequestedTheme="Dark">
<Grid>
<Border x:Name="bor1" HorizontalAlignment="Stretch" Margin="10,10,10,100" VerticalAlignment="Stretch" Background="Gray"/>
<Button x:Name="cmdGetBitmap" Content="Get Bitmap" HorizontalAlignment="Left" Margin="14,0,0,24" VerticalAlignment="Bottom" Width="193" FontSize="18"/>
<Border x:Name="setBrightness" HorizontalAlignment="Left" Margin="306,0,0,24" VerticalAlignment="Bottom" Width="400" Padding="0,4,0,5" Background="#22FFFFFF">
<Grid Margin="2,0,2,0">
<TextBlock FontSize="18" Text="Low" HorizontalAlignment="Left"></TextBlock>
<TextBlock FontSize="18" Text="High" HorizontalAlignment="Right"></TextBlock>
<TextBlock FontSize="18" Text="Brightness" HorizontalAlignment="Center"></TextBlock>
</Grid>
</Border>
</Grid>
</Page>
and the code code:
Imports Windows.Storage
Imports Windows.Storage.Pickers
Imports Windows.Storage.Streams
Imports Windows.UI.Input
Public NotInheritable Class MainPage
Inherits Page
Private allBytes() As Byte, wb1Size As Size
Private Async Sub CmdGetBitmap_Click(sender As Object, e As RoutedEventArgs) Handles cmdGetBitmap.Click
Dim fop1 As New FileOpenPicker
Dim wb1 As WriteableBitmap
fop1.FileTypeFilter.Add(".png")
fop1.FileTypeFilter.Add(".jpg")
fop1.FileTypeFilter.Add(".bmp")
fop1.FileTypeFilter.Add(".tif")
Dim Stream1 As Stream
Dim storageFile1 As StorageFile = Await fop1.PickSingleFileAsync
If storageFile1 Is Nothing Then Exit Sub
Dim fileAccessStream1 As FileRandomAccessStream = Await storageFile1.OpenAsync(FileAccessMode.Read)
wb1 = New WriteableBitmap(6, 6)
'the bitmap that accepts setSourceAsync needs to be initialized, any size will do
'when the source is set, wb1 changes to the correct size for the bitmap picked
Await wb1.SetSourceAsync(fileAccessStream1)
Stream1 = wb1.PixelBuffer.AsStream
ReDim allBytes(Stream1.Length - 1)
Stream1.Read(allBytes, 0, Stream1.Length)
Stream1.Dispose()
bor1.Background = New ImageBrush With {.ImageSource = wb1, .Stretch = Stretch.Uniform}
wb1Size = New Size(wb1.PixelWidth, wb1.PixelHeight) 'save this globally for later
End Sub
Private Async Sub SetBrightness_PointerPressed(sender As Object, e As PointerRoutedEventArgs) Handles setBrightness.PointerPressed
Dim sn1 As Single = setBrightness.ActualWidth / 2
Dim sn3, sn2(), snY, snU, snV As Single
Dim newBytes(allBytes.GetUpperBound(0)), bt2() As Byte
Dim ppt As PointerPoint = e.GetCurrentPoint(setBrightness)
sn3 = ppt.Position.X - sn1
sn3 /= sn1
sn3 *= 200
For lt1 = 0 To allBytes.GetUpperBound(0) Step 4
'the thing what makes brightnesses :)
sn2 = {allBytes(lt1), allBytes(lt1 + 1), allBytes(lt1 + 2)}
snY = sn2(0) * 0.114 + sn2(1) * 0.587 + sn2(2) * 0.299
snU = 0.492 * (sn2(0) - snY)
snV = 0.877 * (sn2(2) - snY)
snY += sn3
sn2(2) = snY + snV * 1.13983
sn2(1) = snY + snU * -0.39465 + snV * -0.5806
sn2(0) = snY + snU * 2.03211
For lt3 = 0 To 2
If sn2(lt3) < 0 Then
sn2(lt3) = 0
ElseIf sn2(lt3) > 255 Then
sn2(lt3) = 255
End If
Next
bt2 = {sn2(0), sn2(1), sn2(2), allBytes(lt1 + 3)}
bt2.CopyTo(newBytes, lt1)
Next
Dim wb1 As WriteableBitmap = New WriteableBitmap(wb1Size.Width, wb1Size.Height)
Dim Stream1 As Stream = wb1.PixelBuffer.AsStream
Await Stream1.WriteAsync(newBytes, 0, Stream1.Length)
Await Stream1.FlushAsync
bor1.Background = New ImageBrush With {.ImageSource = wb1, .Stretch = Stretch.Uniform}
End Sub
End Class
Guess that's it. :)