Skip to content

Commit 37b2614

Browse files
committed
add copy to clipboard
1 parent d90728e commit 37b2614

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
<CommandBinding Command="ApplicationCommands.Undo" Executed="Executed_Undo" CanExecute="CanExecute_Undo"/>
1212
<CommandBinding Command="ApplicationCommands.Redo" Executed="Executed_Redo" CanExecute="CanExecute_Redo"/>
1313
<CommandBinding Command="ApplicationCommands.Paste" Executed="Executed_Paste" CanExecute="CanExecute_Paste"/>
14+
<CommandBinding Command="ApplicationCommands.Copy" Executed="Executed_Copy" CanExecute="CanExecute_Copy"/>
1415
</Window.CommandBindings>
1516
<Window.InputBindings>
1617
<KeyBinding Command="ApplicationCommands.Undo" Gesture="Ctrl+Z"/>
1718
<KeyBinding Command="ApplicationCommands.Redo" Gesture="Ctrl+Y"/>
1819
<KeyBinding Command="ApplicationCommands.Paste" Gesture="Ctrl+V"/>
20+
<KeyBinding Command="ApplicationCommands.Copy" Gesture="Ctrl+C"/>
1921
</Window.InputBindings>
2022
<Window.Resources>
2123

PixelArtTool/MainWindow.xaml.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,16 @@ public void CanExecute_Paste(object sender, CanExecuteRoutedEventArgs e)
825825
e.CanExecute = true;
826826
}
827827

828+
public void Executed_Copy(object sender, ExecutedRoutedEventArgs e)
829+
{
830+
OnCopyImageToClipboard();
831+
}
832+
833+
public void CanExecute_Copy(object sender, CanExecuteRoutedEventArgs e)
834+
{
835+
e.CanExecute = true;
836+
}
837+
828838
// paste image from clipboard to canvas
829839
void OnPasteImageFromClipboard()
830840
{
@@ -878,7 +888,40 @@ void OnPasteImageFromClipboard()
878888
}
879889
}
880890

891+
void OnCopyImageToClipboard()
892+
{
893+
// FIXME no transparency
894+
Clipboard.SetImage(ConvertWriteableBitmapToBitmapImage(canvasBitmap.Clone()));
895+
896+
/*
897+
var bitmap = ConvertWriteableBitmapToBitmapImage(canvasBitmap.Clone());
898+
Stream stream = new MemoryStream();
899+
BitmapEncoder enc = new BmpBitmapEncoder();
900+
enc.Frames.Add(BitmapFrame.Create(bitmap));
901+
enc.Save(stream);
902+
var data = new DataObject("PNG", stream);
903+
Clipboard.Clear();
904+
Clipboard.SetDataObject(data, true);
905+
*/
906+
}
881907

908+
// https://stackoverflow.com/a/14165162/5452781
909+
public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm)
910+
{
911+
BitmapImage bmImage = new BitmapImage();
912+
using (MemoryStream stream = new MemoryStream())
913+
{
914+
PngBitmapEncoder encoder = new PngBitmapEncoder();
915+
encoder.Frames.Add(BitmapFrame.Create(wbm));
916+
encoder.Save(stream);
917+
bmImage.BeginInit();
918+
bmImage.CacheOption = BitmapCacheOption.OnLoad;
919+
bmImage.StreamSource = stream;
920+
bmImage.EndInit();
921+
bmImage.Freeze();
922+
}
923+
return bmImage;
924+
}
882925

883926
void FloodFill(int x, int y, int fillColor)
884927
{

0 commit comments

Comments
 (0)