static int _wpfPayloadCount;     // used to disambiguate between all acts of loading from different WPF payloads.


        internal static object LoadElement(Stream stream)
        {
            Package    package    = Package.Open(stream, FileMode.Open, FileAccess.Read);
            WpfPayload wpfPayload = new WpfPayload(package);

            PackagePart xamlEntryPart = wpfPayload.GetWpfEntryPart();


            int newWpfPayoutCount = _wpfPayloadCount++;
            Uri payloadUri        = new Uri("payload://wpf" + newWpfPayoutCount, UriKind.Absolute);
            Uri entryPartUri      = PackUriHelper.Create(payloadUri, xamlEntryPart.Uri); // gives an absolute uri of the entry part
            Uri packageUri        = PackUriHelper.GetPackageUri(entryPartUri);           // extracts package uri from combined package+part uri

            PackageStore.AddPackage(packageUri, wpfPayload.Package);                     // Register the package
            ParserContext parserContext = new ParserContext();

            parserContext.BaseUri = entryPartUri;

            object xamlObject = XamlReader.Load(xamlEntryPart.GetStream(), parserContext);

            // Remove the temporary uri from the PackageStore
            PackageStore.RemovePackage(packageUri);

            return(xamlObject);
        }
        internal static Stream SaveImage(BitmapSource bitmapSource, string imageContentType)
        {
            MemoryStream stream = new MemoryStream();
            // Create the wpf package in the stream
            WpfPayload wpfPayload = new WpfPayload();

            using (wpfPayload.CreatePackage(stream))
            {
                PackagePart xamlEntryPart = wpfPayload.CreateWpfEntryPart();

                Stream xamlPartStream = xamlEntryPart.GetStream();
                using (xamlPartStream)
                {
                    int    imageIndex     = 0;
                    string imageReference = GetImageReference(GetImageName(imageIndex, imageContentType));

                    StreamWriter xamlPartWriter = new StreamWriter(xamlPartStream);
                    using (xamlPartWriter)
                    {
                        string xamlText =
                            "<Span xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                            "<InlineUIContainer><Image " +
                            "Width=\"" +
                            bitmapSource.Width + "\" " +
                            "Height=\"" +
                            bitmapSource.Height + "\" " +
                            "><Image.Source><BitmapImage CacheOption=\"OnLoad\" UriSource=\"" +
                            imageReference +
                            "\"/></Image.Source></Image></InlineUIContainer></Span>";
                        xamlPartWriter.Write(xamlText);
                    }
                    wpfPayload.CreateImagePart(xamlEntryPart, bitmapSource, imageContentType, imageIndex);
                }
            }
            return(stream);
        }
Beispiel #3
0
        static int _wpfPayloadCount; // used to disambiguate between all acts of loading from different WPF payloads.


        internal static object LoadElement(Stream stream)
        {
            Package package = Package.Open(stream, FileMode.Open, FileAccess.Read);
            WpfPayload wpfPayload = new WpfPayload(package);
            PackagePart xamlEntryPart = wpfPayload.GetWpfEntryPart();

            int newWpfPayoutCount = _wpfPayloadCount++;
            Uri payloadUri = new Uri("payload://wpf" + newWpfPayoutCount, UriKind.Absolute);
            Uri entryPartUri = PackUriHelper.Create(payloadUri, xamlEntryPart.Uri); // gives an absolute uri of the entry part
            Uri packageUri = PackUriHelper.GetPackageUri(entryPartUri); // extracts package uri from combined package+part uri
            PackageStore.AddPackage(packageUri, wpfPayload.Package); // Register the package
            ParserContext parserContext = new ParserContext();
            parserContext.BaseUri = entryPartUri;

            object xamlObject = XamlReader.Load(xamlEntryPart.GetStream(), parserContext);
            // Remove the temporary uri from the PackageStore
            PackageStore.RemovePackage(packageUri);

            return xamlObject;
        }
Beispiel #4
0
        internal static Stream SaveImage(BitmapSource bitmapSource, ImageContentType imageContentType)
        {
            MemoryStream stream = new MemoryStream();
            // Create the wpf package in the stream
            WpfPayload wpfPayload = new WpfPayload();
            using (wpfPayload.CreatePackage(stream))
            {
                PackagePart xamlEntryPart = wpfPayload.CreateWpfEntryPart();

                Stream xamlPartStream = xamlEntryPart.GetStream();
                using (xamlPartStream)
                {
                    // int imageIndex = 0;
                    imageIndex++;
                    string imageReference = GetImageReference(GetImageName(imageIndex, imageContentType));

                    StreamWriter xamlPartWriter = new StreamWriter(xamlPartStream);
                    using (xamlPartWriter)
                    {
                        string xamlText =
                            "<Span xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                            "<InlineUIContainer><Image " +
                            "Width=\"" +
                            bitmapSource.Width + "\" " +
                            "Height=\"" +
                            bitmapSource.Height + "\" " +
                            "><Image.Source><BitmapImage CacheOption=\"OnLoad\" UriSource=\"" +
                            imageReference +
                            "\"/></Image.Source></Image></InlineUIContainer></Span>";
                        xamlPartWriter.Write(xamlText);
                    }
                    wpfPayload.CreateImagePart(xamlEntryPart, bitmapSource, imageContentType, imageIndex);
                }
            }
            return stream;
        }