Skip to content

Commit

Permalink
Support pasting bitmaps
Browse files Browse the repository at this point in the history
Fixes #235
  • Loading branch information
cyanfish committed Jan 10, 2024
1 parent c785204 commit 9015ec7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions NAPS2.Lib.Gtk/EtoForms/Gtk/GtkEtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public override Bitmap ToBitmap(IMemoryImage image)
return new Bitmap(new BitmapHandler(pixbuf));
}

public override IMemoryImage FromBitmap(ImageContext imageContext, Bitmap bitmap)
{
return new GtkImage(imageContext, bitmap.ToGdk());
}

public override void SetClipboardImage(Clipboard clipboard, Bitmap image)
{
// Without cloning the image, Gtk gives errors on paste.
Expand Down
5 changes: 5 additions & 0 deletions NAPS2.Lib.Mac/EtoForms/Mac/MacEtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public override Bitmap ToBitmap(IMemoryImage image)
return new Bitmap(new BitmapHandler((NSImage) nsImage.Copy()));
}

public override IMemoryImage FromBitmap(ImageContext imageContext, Bitmap bitmap)
{
return new MacImage(imageContext, bitmap.ToNS());
}

public override IMemoryImage DrawHourglass(ImageContext imageContext, IMemoryImage image)
{
// TODO
Expand Down
5 changes: 5 additions & 0 deletions NAPS2.Lib.WinForms/EtoForms/WinForms/WinFormsEtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public override Bitmap ToBitmap(IMemoryImage image)
return bitmap.ToEto();
}

public override IMemoryImage FromBitmap(ImageContext imageContext, Bitmap bitmap)
{
return new GdiImage(imageContext, (SD.Bitmap) bitmap.ToSD());
}

public override IMemoryImage DrawHourglass(ImageContext imageContext, IMemoryImage image)
{
var bitmap = new System.Drawing.Bitmap(image.Width, image.Height);
Expand Down
13 changes: 13 additions & 0 deletions NAPS2.Lib/EtoForms/Desktop/DesktopController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using Eto.Drawing;
using Eto.Forms;
using NAPS2.EtoForms.Notifications;
using NAPS2.ImportExport;
Expand Down Expand Up @@ -346,6 +347,18 @@ public void Paste()
{
ImportDirect(_imageTransfer.GetFromClipboard(), true);
}
else if (Clipboard.Instance.ContainsImage)
{
var etoBitmap = (Bitmap) Clipboard.Instance.Image;
Task.Run(() =>
{
var image = EtoPlatform.Current.FromBitmap(_scanningContext.ImageContext, etoBitmap);
var processedImage = _scanningContext.CreateProcessedImage(image);
processedImage = ImportPostProcessor.AddPostProcessingData(processedImage, image,
_thumbnailController.RenderSize, new BarcodeDetectionOptions(), true);
_desktopImagesController.ReceiveScannedImage()(processedImage);
});
}
}

public async Task Copy()
Expand Down
1 change: 1 addition & 0 deletions NAPS2.Lib/EtoForms/EtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static EtoPlatform Current
public abstract IListView<T> CreateListView<T>(ListViewBehavior<T> behavior) where T : notnull;
public abstract void ConfigureImageButton(Button button, bool big);
public abstract Bitmap ToBitmap(IMemoryImage image);
public abstract IMemoryImage FromBitmap(ImageContext imageContext, Bitmap bitmap);
public abstract IMemoryImage DrawHourglass(ImageContext imageContext, IMemoryImage thumb);
public abstract void SetFrame(Control container, Control control, Point location, Size size, bool inOverlay);
public abstract Control CreateContainer();
Expand Down
2 changes: 1 addition & 1 deletion NAPS2.Lib/EtoForms/Ui/DesktopForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void OpeningContextMenu(object? sender, EventArgs e)
if (!EtoPlatform.Current.IsMac)
{
// TODO: Can't do this on Mac yet as it disables the menu item indefinitely
Commands.Paste.Enabled = _imageTransfer.IsInClipboard();
Commands.Paste.Enabled = _imageTransfer.IsInClipboard() || Clipboard.Instance.ContainsImage;
}
if (ImageList.Selection.Any())
{
Expand Down

0 comments on commit 9015ec7

Please sign in to comment.