Microsoft KB Archive/942847

= Using RenderTargetBitmap.Render when Printing an XPS Document Renders with an Incorrect Size =

Article ID: 942847

Article Last Modified on 9/21/2007

-

APPLIES TO


 * Microsoft .NET Framework 3.0

-



Source: Microsoft Support



RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.



Action
When attempting to use a VisualBrush to render an XPS document, you may notice that the position and size of the XPS document displayed is inconsistent when comparing the results with that of the XPSViewer or with the rendering using a Visual object.



Result
When using a Visual object to render the XPS document, the XPS document will be rendered as expected and is consistent with the image displayed by the XPSViewer. However, when using a VisualBrush, you may see that the image is drawn too large or see that parts of the image which should be clipped are not clipped.



Cause
By default, the VisualBrush will render the entire tree you specify. The XPSViewer will clip the content to the page size. VisualBrush, however, will display the entire image, making the actual size of the rendered subtree larger than the page size. This is correct, because the VisualBrush will render the entire subtree and that does not contain any clip information from the parent.



Resolution
Configure the VisualBrush to only render the desired area:

VisualBrush vb = new VisualBrush(docPage.Visual);

vb.ViewboxUnits = BrushMappingMode.Absolute;

vb.Viewbox = new Rect(docPage.Size);

rectangle.Fill = vb;

Note: the VisualBrush’s default behavior is to stretch to the shape it is applied to. You can change this behavior too, by setting the ViewportUnits and Viewport properties.



MoreInformation
Here is sample code which will use a RenderTargetBitmap and VisualBrush to render the XPS document correctly on a Windows Form.

public partial class Form1 : Form

{

XpsDocument xpsDocument;

FixedDocumentSequence docSeq;

DocumentPage docPage;

public Form1

{

InitializeComponent;

xpsDocument = new XpsDocument(&quot;Test.xps&quot;, FileAccess.Read);

docSeq = xpsDocument.GetFixedDocumentSequence;

docPage = docSeq.DocumentPaginator.GetPage(0);

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

RenderTargetBitmap rtb = new RenderTargetBitmap((int)docPage.Size.Width, (int)docPage.Size.Height, 96, 96, PixelFormats.Default);

System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle;

VisualBrush vb = new VisualBrush(docPage.Visual);

vb.ViewboxUnits = BrushMappingMode.Absolute;

vb.Viewbox = new Rect(docPage.Size);

rectangle.Fill = vb;

System.Windows.Size rsize1 = new System.Windows.Size(docPage.Size.Width, docPage.Size.Height);

rectangle.Measure(rsize1);

System.Windows.Rect rect = new System.Windows.Rect(0, 0, docPage.Size.Width, docPage.Size.Height);

rectangle.Arrange(rect);

rectangle.UpdateLayout;

rtb.Render(rectangle);

// Render to the Graphics object

Byte[] rgbValues = new Byte[rtb.PixelWidth * 4 * rtb.PixelHeight];

rtb.CopyPixels(rgbValues, rtb.PixelWidth * 4, 0);

using (Bitmap bmp = new Bitmap(rtb.PixelWidth, rtb.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb))

{

System.Drawing.Imaging.BitmapData bd = bmp.LockBits(new Rectangle(0, 0, rtb.PixelWidth, rtb.PixelHeight),

System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bd.Scan0, rgbValues.Length);

bmp.UnlockBits(bd);

e.Graphics.DrawImage(bmp, 0, 0);

}

}

}



DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Keywords: kbnomt kbrapidpub KB942847

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.