Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder image fetching for GetImageFromClipboard to try PNG first #1344

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- [SIL.Windows.Forms] Look for PNG data on clipboard before checking for plain image in WindowsClipboard.GetImageFromClipboard() in order to preserve transparency in copied images.
- [SIL.Windows.Forms] Changed layout of SILAboutBox to accommodate wider SIL logo.
- [SIL.Windows.Forms.Archiving] Split SIL.Archiving, moving Winforms portions (including dependency on L10nSharp) to SIL.Windows.Forms.Archiving.
- [SIL.Archiving] Changed IMDIArchivingDlgViewModel.ArchivingPackage to return an IMDIPackage (instead of an IArchivingPackage).
Expand Down
33 changes: 17 additions & 16 deletions SIL.Windows.Forms/Clipboarding/WindowsClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public PalasoImage GetImageFromClipboard()
var textData = string.Empty;
if (dataObject.GetDataPresent(DataFormats.UnicodeText))
textData = dataObject.GetData(DataFormats.UnicodeText) as string;

// The order of operations here is a bit tricky.
// We want to maintain transparency in the original image, so we need to use the PNG format.
// Clipboard.ContainsImage() and Clipboard.GetImage() use the plain bitmap format which loses
// transparency. So here we explicitly ask for a PNG, and see if we can get it.
if (dataObject.GetDataPresent("PNG"))
{
var o = dataObject.GetData("PNG") as Stream;
try
{
return PalasoImage.FromImage(Image.FromStream(o));
}
catch (Exception e)
{
ex = e;
}
}
if (Clipboard.ContainsImage())
{
PalasoImage plainImage = null;
Expand Down Expand Up @@ -91,22 +108,6 @@ public PalasoImage GetImageFromClipboard()
throw;
}
}
// the ContainsImage() returns false when copying an PNG from MS Word
// so here we explicitly ask for a PNG and see if we can convert it.
if (dataObject.GetDataPresent("PNG"))
{
var o = dataObject.GetData("PNG") as Stream;
try
{
return PalasoImage.FromImage(Image.FromStream(o));
}
catch (Exception e)
{
// I'm not sure why, but previous versions of this code would continue trying other
// options at this point.
ex = e;
}
}

// People can do a "copy" from the Windows Photo Viewer but what it puts on the System.Windows.Forms.Clipboard is a path, not an image
if (dataObject.GetDataPresent(DataFormats.FileDrop))
Expand Down
Loading