Skip to content

Commit d90728e

Browse files
committed
add paste images from clipboard
1 parent a515d89 commit d90728e

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
<Window.CommandBindings>
1111
<CommandBinding Command="ApplicationCommands.Undo" Executed="Executed_Undo" CanExecute="CanExecute_Undo"/>
1212
<CommandBinding Command="ApplicationCommands.Redo" Executed="Executed_Redo" CanExecute="CanExecute_Redo"/>
13+
<CommandBinding Command="ApplicationCommands.Paste" Executed="Executed_Paste" CanExecute="CanExecute_Paste"/>
1314
</Window.CommandBindings>
1415
<Window.InputBindings>
1516
<KeyBinding Command="ApplicationCommands.Undo" Gesture="Ctrl+Z"/>
1617
<KeyBinding Command="ApplicationCommands.Redo" Gesture="Ctrl+Y"/>
18+
<KeyBinding Command="ApplicationCommands.Paste" Gesture="Ctrl+V"/>
1719
</Window.InputBindings>
1820
<Window.Resources>
1921

PixelArtTool/MainWindow.xaml.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using System.IO;
77
using System.Linq;
88
using System.Runtime.CompilerServices;
9+
using System.Runtime.InteropServices;
910
using System.Windows;
1011
using System.Windows.Controls;
1112
using System.Windows.Input;
13+
using System.Windows.Interop;
1214
using System.Windows.Media;
1315
using System.Windows.Media.Imaging;
1416
using static PixelArtTool.Tools;
@@ -813,6 +815,71 @@ public void CanExecute_Redo(object sender, CanExecuteRoutedEventArgs e)
813815
e.CanExecute = true;
814816
}
815817

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+
816883
void FloodFill(int x, int y, int fillColor)
817884
{
818885
// get hit color pixel
@@ -1099,6 +1166,9 @@ private void OnGetTransparentColorButton(object sender, MouseButtonEventArgs e)
10991166
rectCurrentColor.Fill = new SolidColorBrush(Color.FromArgb(c.Alpha, c.Red, c.Green, c.Blue));
11001167
ResetCurrentBrightnessPreview(currentColor);
11011168
}
1169+
1170+
1171+
11021172
} // class
11031173

11041174
} // namespace

0 commit comments

Comments
 (0)