Skip to content

Commit

Permalink
Merge pull request #1344 from sillsdev/BL-13810-PreserveTransparentPa…
Browse files Browse the repository at this point in the history
…sting

Reorder image fetching for GetImageFromClipboard to try PNG first
  • Loading branch information
hatton authored Aug 28, 2024
2 parents 3fa5695 + 9bf7b61 commit e4a1935
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
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

0 comments on commit e4a1935

Please sign in to comment.