@@ -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