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); }
SerializeColorContext( IServiceProvider context, ColorContext colorContext ) { Uri profileUri = null; if (colorContext == null) { throw new ArgumentNullException("colorContext"); } if (context != null) { PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager)); Dictionary <int, Uri> colorContextTable = manager.ResourcePolicy.ColorContextTable; Dictionary <int, Uri> currentPageColorContextTable = manager.ResourcePolicy.CurrentPageColorContextTable; if (currentPageColorContextTable == null) { // // throw the appropriae exception // } if (colorContextTable == null) { // // throw the appropriae exception // } if (colorContextTable.ContainsKey(colorContext.GetHashCode())) { // // The colorContext has already been cached (and therefore serialized). // No need to serialize it again so we just use the Uri in the // package where the original was serialized. For that Uri used // a relationship is only created if this has not been included on // the current page before. // profileUri = colorContextTable[colorContext.GetHashCode()]; if (!currentPageColorContextTable.ContainsKey(colorContext.GetHashCode())) { // // Also, add a relationship for the current page to this Color Context // resource. This is needed to conform with Xps specification. // manager.AddRelationshipToCurrentPage(profileUri, XpsS0Markup.ResourceRelationshipName); currentPageColorContextTable.Add(colorContext.GetHashCode(), profileUri); } } else { MS.Internal.ContentType colorContextMimeType = XpsS0Markup.ColorContextContentType; XpsResourceStream resourceStream = manager.AcquireResourceStream(typeof(ColorContext), colorContextMimeType.ToString()); byte [] buffer = new byte[512]; Stream profileStream = colorContext.OpenProfileStream(); int count; while ((count = profileStream.Read(buffer, 0, buffer.GetLength(0))) > 0) { resourceStream.Stream.Write(buffer, 0, count); } profileStream.Close(); // // Make sure to commit the resource stream by releasing it. // profileUri = resourceStream.Uri; manager.ReleaseResourceStream(typeof(ColorContext)); colorContextTable.Add(colorContext.GetHashCode(), profileUri); currentPageColorContextTable.Add(colorContext.GetHashCode(), profileUri); } } else // Testing only { profileUri = colorContext.ProfileUri; } //First Step make sure that nothing that should not be escaped is escaped Uri safeUnescapedUri = new Uri(profileUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.SafeUnescaped), profileUri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative); //Second Step make sure that everything that should escaped is escaped String uriString = safeUnescapedUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped); return(uriString); }