Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipboard + PresentationFramework = OutOfMemoryException?

Why does System.Windows.Clipboard(PresentationCore.dll) is not friendly with the System.Windows.Thickness (PresentationFramework.dll) but friendly with System.Windows.Point (WindowsBase.dll)

using System;
namespace ConsoleApplication5 {
    class Program {
        /*
        * Add references to 
        * WindowsBase.dll (Point)
        * PresentationFramework.dll (Thickness)
        * PresentationCore.dll (Clipboard)
        */

        [STAThread]
        static void Main(string[] args)
        {
            Test myTest = new Test();
            System.Windows.Clipboard.SetData("myformat", myTest);
            // OutOfMemoryException
            Object myPastedTest = System.Windows.Clipboard.GetData("myformat");
        }
    }

    [Serializable]
    class Test
    {
        // COMMENT THE LINES BELLOW PresentationFramework TO WORK OK!
        // PresentationFramework.dll
        //public System.Windows.Thickness MyThickness { get; set; } 
        public System.Windows.Media.Brush MyBrush { get; set; }

        // WindowsBase.dll
        public System.Windows.Point MyPoint { get; set; }

    }
}
like image 312
serhio Avatar asked Nov 30 '25 02:11

serhio


1 Answers

The Brush class isn't serializable and causes the OutOfMemoryException to be thrown (see http://www.grumpydev.com/2009/09/05/system-outofmemoryexception-gotcha-using-clipboard-getdata-in-wpf/). You might be able to serialize the Test class to XAML and put that onto the clipboard instead, see this link for info:

How can i serialize xaml "Brush"?

like image 199
Mike Payne Avatar answered Dec 02 '25 18:12

Mike Payne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!