Skip to content

Instantly share code, notes, and snippets.

@rbartholomew
Created December 6, 2011 01:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbartholomew/1436266 to your computer and use it in GitHub Desktop.
Save rbartholomew/1436266 to your computer and use it in GitHub Desktop.
Windows 8 Photo Retouching
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;
}
}
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;
}
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;
}
}
<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>
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;
}
}
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment