コード例 #1
0
        ConvertTo(
            ITypeDescriptorContext context,
            System.Globalization.CultureInfo culture,
            object value,
            Type destinationType
            )
        {
            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertImageBegin);

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (!IsSupportedType(destinationType))
            {
                throw new NotSupportedException(SR.Get(SRID.Converter_ConvertToNotSupported));
            }

            //
            // Check that we have a valid BitmapSource instance.
            //
            BitmapSource bitmapSource = (BitmapSource)value;

            if (bitmapSource == null)
            {
                throw new ArgumentException(SR.Get(SRID.MustBeOfType, "value", "BitmapSource"));
            }

            //
            // Get the current serialization manager.
            //
            PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));

            //Get the image Uri if it has already been serialized
            Uri imageUri = GetBitmapSourceFromImageTable(manager, bitmapSource);

            //
            // Get the current page image cache
            //
            Dictionary <int, Uri> currentPageImageTable = manager.ResourcePolicy.CurrentPageImageTable;

            if (imageUri != null)
            {
                int uriHashCode = imageUri.GetHashCode();
                if (!currentPageImageTable.ContainsKey(uriHashCode))
                {
                    //
                    // Also, add a relationship for the current page to this image
                    // resource.  This is needed to conform with Xps specification.
                    //
                    manager.AddRelationshipToCurrentPage(imageUri, XpsS0Markup.ResourceRelationshipName);
                    currentPageImageTable.Add(uriHashCode, imageUri);
                }
            }
            else
            {
                //
                // This image as never serialized before so we will do it now.
                // Retrieve the image serialization service from the resource policy
                //
                IServiceProvider resourceServiceProvider = manager.ResourcePolicy;

                XpsImageSerializationService imageService = (XpsImageSerializationService)resourceServiceProvider.GetService(typeof(XpsImageSerializationService));
                if (imageService == null)
                {
                    throw new XpsSerializationException(SR.Get(SRID.ReachSerialization_NoImageService));
                }

                //
                // Obtain a valid encoder for the image.
                //
                BitmapEncoder encoder       = imageService.GetEncoder(bitmapSource);
                string        imageMimeType = GetImageMimeType(encoder);

                bool isSupportedMimeType = imageService.IsSupportedMimeType(bitmapSource);

                //
                // Acquire a writable stream from the serialization manager and encode
                // and serialize the image into the stream.
                //
                XpsResourceStream resourceStream = manager.AcquireResourceStream(typeof(BitmapSource), imageMimeType);
                bool bCopiedStream = false;

                BitmapFrame bitmapFrame = bitmapSource as BitmapFrame;

                if (isSupportedMimeType &&
                    bitmapFrame != null &&
                    bitmapFrame.Decoder != null
                    )
                {
                    BitmapDecoder decoder = bitmapFrame.Decoder;
                    try
                    {
                        Uri    sourceUri = new Uri(decoder.ToString());
                        Stream srcStream = MS.Internal.WpfWebRequestHelper.CreateRequestAndGetResponseStream(sourceUri);
                        CopyImageStream(srcStream, resourceStream.Stream);
                        srcStream.Close();
                        bCopiedStream = true;
                    }
                    catch (UriFormatException)
                    {
                        //the uri was not valid we will re-encode the image below
                    }
                    catch (WebException)
                    {
                        //Web Request failed we will re-encode the image below
                    }
                }

                if (!bCopiedStream)
                {
                    Stream stream = new MemoryStream();
                    ReEncodeImage(bitmapSource, encoder, stream);
                    stream.Position = 0;
                    CopyImageStream(stream, resourceStream.Stream);
                }

                //
                // Make sure to commit the resource stream by releasing it.
                //
                imageUri = resourceStream.Uri;
                manager.ReleaseResourceStream(typeof(BitmapSource));

                AddBitmapSourceToImageTables(manager, imageUri);
            }

            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertImageEnd);

            return(imageUri);
        }