internal static MemoryStream SaveImage(BitmapSource bitmapSource, string imageContentType) { MemoryStream memoryStream = new MemoryStream(); WpfPayload wpfPayload = new WpfPayload(null); using (wpfPayload.CreatePackage(memoryStream)) { int imageIndex = 0; string imageReference = WpfPayload.GetImageReference(WpfPayload.GetImageName(imageIndex, imageContentType)); PackagePart packagePart = wpfPayload.CreateWpfEntryPart(); Stream stream = packagePart.GetStream(); using (stream) { StreamWriter streamWriter = new StreamWriter(stream); using (streamWriter) { string value = string.Concat(new object[] { "<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>" }); streamWriter.Write(value); } } wpfPayload.CreateImagePart(packagePart, bitmapSource, imageContentType, imageIndex); } return(memoryStream); }
internal static MemoryStream SaveImage(BitmapSource bitmapSource, string imageContentType) { MemoryStream stream = new MemoryStream(); // Create the wpf package in the stream WpfPayload wpfPayload = new WpfPayload(/*package:*/ null); // Create a package in the stream using (wpfPayload.CreatePackage(stream)) { // Define a reference for the image int imageIndex = 0; string imageReference = GetImageReference(GetImageName(imageIndex, imageContentType)); // Create the entry part for xaml content of the WPF package PackagePart xamlEntryPart = wpfPayload.CreateWpfEntryPart(); // Write the part's content Stream xamlPartStream = xamlEntryPart.GetStream(); using (xamlPartStream) { 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); } } // Add image to a package wpfPayload.CreateImagePart(xamlEntryPart, bitmapSource, imageContentType, imageIndex); } return(stream); }
internal static MemoryStream SaveImage(BitmapSource bitmapSource, string imageContentType) { MemoryStream stream = new MemoryStream(); // Create the wpf package in the stream WpfPayload wpfPayload = new WpfPayload(/*package:*/null); // Create a package in the stream using (wpfPayload.CreatePackage(stream)) { // Define a reference for the image int imageIndex = 0; string imageReference = GetImageReference(GetImageName(imageIndex, imageContentType)); // Create the entry part for xaml content of the WPF package PackagePart xamlEntryPart = wpfPayload.CreateWpfEntryPart(); // Write the part's content Stream xamlPartStream = xamlEntryPart.GetStream(); using (xamlPartStream) { 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); } } // Add image to a package wpfPayload.CreateImagePart(xamlEntryPart, bitmapSource, imageContentType, imageIndex); } return stream; }