Created
January 29, 2018 03:20
-
-
Save meziantou/fdced91c6c012d8ca137b4ec14ecdf85 to your computer and use it in GitHub Desktop.
Paste images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void OnPaste(object sender, DataObjectPastingEventArgs e) | |
{ | |
HandleDataObject(e.DataObject); | |
e.Handled = true; | |
} | |
private void HandleDataObject(IDataObject data) | |
{ | |
if (data == null) throw new ArgumentNullException(nameof(data)); | |
var formats = data.GetFormats(); | |
if (data.GetDataPresent(DataFormats.FileDrop)) | |
{ | |
string[] files = (string[])data.GetData(DataFormats.FileDrop, true); | |
if (files != null) | |
{ | |
foreach (var file in files) | |
{ | |
} | |
} | |
} | |
else if (data.GetDataPresent("PNG")) // Paint.NET | |
{ | |
var pngData = data.GetData("PNG"); | |
var stream = pngData as Stream; | |
if (stream == null) | |
return; | |
using (FileStream fs = File.Create("Img.png")) | |
{ | |
stream.CopyTo(fs); | |
} | |
} | |
else if (data.GetDataPresent(DataFormats.Bitmap)) | |
{ | |
BitmapSource bitmap = data.GetData(DataFormats.Bitmap) as InteropBitmap; | |
if (bitmap == null) | |
return; | |
bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgr24, bitmap.Palette, 0); | |
using (FileStream stream = File.Create("Img.png")) | |
{ | |
var encoder = new PngBitmapEncoder(); | |
encoder.Interlace = PngInterlaceOption.On; | |
var bitmapFrame = BitmapFrame.Create(bitmap); | |
encoder.Frames.Add(bitmapFrame); | |
encoder.Save(stream); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment