public
Created

Windows 8 Photo Retouching

  • Download Gist
capturephoto.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13
private async void CapturePhoto_Click(object sender, RoutedEventArgs e)
{
var dialog = new Windows.Media.Capture.CameraCaptureUI();
photo = await dialog.CaptureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.Photo);
 
if (photo != null)
{
IRandomAccessStream stream = await photo.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
image = new BitmapImage();
image.SetSource(stream);
PhotoHolder.Source = image;
}
}
grayscale.cs
C#
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
private void BWConverstion_Click(object sender, RoutedEventArgs e)
{
if (photo != null)
{
bitmap = new WriteableBitmap(image);
var stream = bitmap.PixelBuffer.AsStream();
byte[] buffer = new byte[(int)stream.Length];
int c = stream.Read(buffer, 0, (int)stream.Length);
 
buffer = BlackWhiteConversion(buffer);
stream.Seek(0, System.IO.SeekOrigin.Begin);
stream.Write(buffer, 0, buffer.Length);
bitmap.Invalidate();
 
PhotoHolder.Source = bitmap;
}
}
 
private byte[] BlackWhiteConversion(byte[] buffer)
{
for (int i = 0; i < buffer.Length; i += 4)
{
byte r = buffer[i];
byte g = buffer[i + 1];
byte b = buffer[i + 2];
byte gray = (byte)Math.Round(.299 * r + .587 * g + .114 * b);
buffer[i] = buffer[i + 1] = buffer[i + 2] = gray;
}
return buffer;
}
invert.cs
C#
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
private void Invert_Click(object sender, RoutedEventArgs e)
{
if (image != null)
{
bitmap = new WriteableBitmap(image);
var stream = bitmap.PixelBuffer.AsStream();
 
byte[] buffer = new byte[(int)stream.Length];
int c = stream.Read(buffer, 0, (int)stream.Length);
 
if (c > 0)
{
for (int i = 0; i < buffer.Length; i += 4)
{
buffer[i] = (byte)(255 - buffer[i]);
buffer[i + 1] = (byte)(255 - buffer[i + 1]);
buffer[i + 2] = (byte)(255 - buffer[i + 2]);
}
}
 
stream.Seek(0, System.IO.SeekOrigin.Begin);
stream.Write(buffer, 0, buffer.Length);
bitmap.Invalidate();
 
PhotoHolder.Source = bitmap;
}
}
mainpage.xaml
XML
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
<UserControl.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="10,50,0,0"></Setter>
<Setter Property="Width" Value="150"></Setter>
</Style>
<Style x:Key="AppButton" TargetType="Button">
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="Margin" Value="0,0,10,15"></Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="80"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel>
<Button x:Name="BWConverstion" Click="BWConverstion_Click" Content="Black and White"></Button>
<Button x:Name="Invert" Click="Invert_Click" Content="Invert"></Button>
</StackPanel>
<Border Grid.Column="1" Margin="50,50,0,0" BorderBrush="White" BorderThickness="2">
<Image x:Name="PhotoHolder"></Image>
</Border>
<ApplicationBar Grid.Row="1" Grid.ColumnSpan="2">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,25,0">
<Button x:Name="CapturePhoto" Click="CapturePhoto_Click" Style="{StaticResource AppButton}">
<Image Source="/Icons/Camera.png"></Image>
</Button>
<Button x:Name="OpenPhoto" Click="OpenPhoto_Click" Style="{StaticResource AppButton}">
<Image Source="/Icons/Open.png"></Image>
</Button>
<Button x:Name="SavePhoto" Click="SavePhoto_Click" Style="{StaticResource AppButton}">
<Image Source="/Icons/Save.png"></Image>
</Button>
</StackPanel>
</ApplicationBar>
</Grid>
openfile.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
async void OpenPhoto_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
 
if (file != null)
{
photo = file;
IRandomAccessStream stream = await photo.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
image = new BitmapImage();
image.SetSource(stream);
PhotoHolder.Source = image;
}
}
saveimage.cs
C#
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
async void SavePhoto_Click(object sender, RoutedEventArgs e)
{
if (photo != null)
{
if (bitmap == null)
{
bitmap = new WriteableBitmap(image);
}
FileSavePicker picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeChoices.Add("PNG", new List<string>() { ".png" });
picker.DefaultFileExtension = ".png";
picker.SuggestedFileName = "photo";
StorageFile savedFile = await picker.PickSaveFileAsync();
 
if (savedFile != null)
{
IRandomAccessStream output = await savedFile.OpenAsync(FileAccessMode.ReadWrite);
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, output);
 
var stream = bitmap.PixelBuffer.AsStream();
byte[] buffer = new byte[(int)stream.Length];
int c = stream.Read(buffer, 0, (int)stream.Length);
 
encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight,
96.0, 96.0, buffer);
await encoder.FlushAsync();
output.GetOutputStreamAt(0).FlushAsync().Start();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.