Jump to content

Clipboard and Transparency


ArgusMagnus
 Share

Recommended Posts

I've read in an old thread (can't remeber which one) that starting with version 4.0 Paint.net will support copying/pasting images which contain transparency.

1. Is this correct?

2. Is the facility which achieves this available for plugin authors? If yes, how?

3. Somewhat related: What is the difference between using the classes PaintDotNet.PdnClipboard and System.Windows.Forms.Clipboard?

 

My batch Image Processor: https://imagenator.codeplex.com

Link to comment
Share on other sites

Thanks! I knew that MS Office used "PNG" but it didn't occur to me that Paint.net might be as well.
 
I have put together a helper method which takes care of various possibilites to get an image from the clipboard, these are part of the Utils class in my ArgusPaintNet.Shared.dll:

public static Image GetImageFromClipboard()
{
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
return Utils.GetImageFromClipboardCore();
Image image = null;
Thread thread = new Thread(() => { image = Utils.GetImageFromClipboardCore(); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return image;
}

static Image GetImageFromClipboardCore()
{
using (MemoryStream ms = Clipboard.GetData("PNG") as MemoryStream)
{
if (ms != null)
{
try { return Image.FromStream(ms); }
catch { }
}
}
StringCollection paths = Clipboard.GetFileDropList();
if (paths != null && paths.Count > 0)
{
foreach (string file in paths)
{
try { return Image.FromFile(file); }
catch { }
}
}
byte[] data;
using (MemoryStream ms = Clipboard.GetData(DataFormats.Dib) as MemoryStream)
{
if (ms == null)
{
return Clipboard.GetImage();
}
data = ms.ToArray();
}
int width = BitConverter.ToInt32(data, 4);
int stride = width * 4;
int height = BitConverter.ToInt32(data, 8);
int bpp = BitConverter.ToInt16(data, 14);
if (bpp != 32)
return Clipboard.GetImage();
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
IntPtr ptr = new IntPtr((long)handle.AddrOfPinnedObject() + 40);
return new Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, ptr);
}
catch
{
return Clipboard.GetImage();
}
finally
{
handle.Free();
}
}

 

 

Edited by ArgusMagnus

My batch Image Processor: https://imagenator.codeplex.com

Link to comment
Share on other sites

PdnClipboard served as a wrapper around the Clipboard when I was trying to hunt down a bug in the WinForms implementation that was causing crashes. So this let me experiment with wrapping the WPF Clipboard implementation, or having my own implementation, and switching between all 3 to see which one was most stable.

 

In general, please don't use random things you find lying around in Core.dll. It creates dependencies that I can't refactor away without breaking your plugin.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

Thanks for the clarification Rick.

 

As I said, I have a working solution (without using PdnClipboard ;) )

 

Regarding the use of "random things you find lying around in Core.dll": I find it hard to tell what is supposed to be used by plugin developers and what not, since nothing is really documented anywhere (I understand that, writing a useful documentation is time consuming), I learned most things from looking at other plugins' code and VS' Intellisense and Object Browser. For instance it is clear, that the ColorBgra structure is supposed to be used, but what about RgbColor, RgbColorF, HsvColor, HsvColorF? Maybe adding something like the following to each class/struct which is supposed to be used may be an option?

/// <summary>
/// Public API
/// </summary>
public struct ColorBgra { ... }

My batch Image Processor: https://imagenator.codeplex.com

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...