|
6 | 6 | using System.IO;
|
7 | 7 | using System.Linq;
|
8 | 8 | using System.Runtime.CompilerServices;
|
| 9 | +using System.Runtime.InteropServices; |
9 | 10 | using System.Windows;
|
10 | 11 | using System.Windows.Controls;
|
11 | 12 | using System.Windows.Input;
|
| 13 | +using System.Windows.Interop; |
12 | 14 | using System.Windows.Media;
|
13 | 15 | using System.Windows.Media.Imaging;
|
14 | 16 | using static PixelArtTool.Tools;
|
@@ -813,6 +815,71 @@ public void CanExecute_Redo(object sender, CanExecuteRoutedEventArgs e)
|
813 | 815 | e.CanExecute = true;
|
814 | 816 | }
|
815 | 817 |
|
| 818 | + public void Executed_Paste(object sender, ExecutedRoutedEventArgs e) |
| 819 | + { |
| 820 | + OnPasteImageFromClipboard(); |
| 821 | + } |
| 822 | + |
| 823 | + public void CanExecute_Paste(object sender, CanExecuteRoutedEventArgs e) |
| 824 | + { |
| 825 | + e.CanExecute = true; |
| 826 | + } |
| 827 | + |
| 828 | + // paste image from clipboard to canvas |
| 829 | + void OnPasteImageFromClipboard() |
| 830 | + { |
| 831 | + if (Clipboard.ContainsImage()) |
| 832 | + { |
| 833 | + IDataObject clipboardData = Clipboard.GetDataObject(); |
| 834 | + |
| 835 | + // https://markheath.net/post/save-clipboard-image-to-file |
| 836 | + if (clipboardData != null) |
| 837 | + { |
| 838 | + BitmapSource source = Clipboard.GetImage(); |
| 839 | + |
| 840 | + // https://stackoverflow.com/questions/5867657/copying-from-bitmapsource-to-writablebitmap |
| 841 | + // Calculate stride of source |
| 842 | + int stride = source.PixelWidth * (source.Format.BitsPerPixel + 7) / 8; |
| 843 | + Console.WriteLine("stride:" + stride); |
| 844 | + // Create data array to hold source pixel data |
| 845 | + byte[] data = new byte[stride * source.PixelHeight]; |
| 846 | + |
| 847 | + // Copy source image pixels to the data array |
| 848 | + source.CopyPixels(data, stride, 0); |
| 849 | + |
| 850 | + // Create WriteableBitmap to copy the pixel data to. |
| 851 | + WriteableBitmap target = new WriteableBitmap( |
| 852 | + source.PixelWidth, |
| 853 | + source.PixelHeight, |
| 854 | + source.DpiX, source.DpiY, |
| 855 | + source.Format, null); |
| 856 | + |
| 857 | + // Write the pixel data to the WriteableBitmap. |
| 858 | + target.WritePixels( |
| 859 | + new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), |
| 860 | + data, stride, 0); |
| 861 | + |
| 862 | + PixelColor c = new PixelColor(); |
| 863 | + for (int x = 0; x < canvasResolutionX; x++) |
| 864 | + { |
| 865 | + for (int y = 0; y < canvasResolutionY; y++) |
| 866 | + { |
| 867 | + var cc = GetPixelColor(x, y, target); |
| 868 | + Console.WriteLine(); |
| 869 | + var ccc = new PixelColor(); |
| 870 | + ccc.Red = cc.Red; |
| 871 | + ccc.Green = cc.Green; |
| 872 | + ccc.Blue = cc.Blue; |
| 873 | + ccc.Alpha = 255; |
| 874 | + SetPixel(canvasBitmap, x, y, (int)ccc.ColorBGRA); |
| 875 | + } |
| 876 | + } |
| 877 | + } |
| 878 | + } |
| 879 | + } |
| 880 | + |
| 881 | + |
| 882 | + |
816 | 883 | void FloodFill(int x, int y, int fillColor)
|
817 | 884 | {
|
818 | 885 | // get hit color pixel
|
@@ -1099,6 +1166,9 @@ private void OnGetTransparentColorButton(object sender, MouseButtonEventArgs e)
|
1099 | 1166 | rectCurrentColor.Fill = new SolidColorBrush(Color.FromArgb(c.Alpha, c.Red, c.Green, c.Blue));
|
1100 | 1167 | ResetCurrentBrightnessPreview(currentColor);
|
1101 | 1168 | }
|
| 1169 | + |
| 1170 | + |
| 1171 | + |
1102 | 1172 | } // class
|
1103 | 1173 |
|
1104 | 1174 | } // namespace
|
0 commit comments