我正在尝试使用here描述的策略打印大型WPF画布。
使用Letter或A4纸张尺寸时,一切正常。但是,当我选择以A2纸张尺寸打印时,我注意到部分图像被剪掉了。任何想法为什么会发生这种情况?
以下是打印的重要部分(至少我认为是)的代码:
private void CreateDocument()
{
DesignerCanvas designerCanvas = _board.DesignerCanvas;
using (new WaitCursor())
{
var paperSize = PaperSizeComboBox.SelectionBoxItem as PageSize;
if (paperSize == null)
{
throw new Exception("Paper Size cannot be null");
}
bool portrait = OrientationComboBox.SelectedIndex == 0;
double zoom = ZoomSlider.Value / 100;
_printDialog.PrintTicket = _printDialog.PrintQueue.DefaultPrintTicket;
_printDialog.PrintTicket.PageMediaSize = paperSize.PageMediaSize;
_printDialog.PrintTicket.PageOrientation = (portrait)
? PageOrientation.Portrait
: PageOrientation.Landscape;
_printDialog.PrintQueue.GetPrintCapabilities();
var fixedDocument = new FixedDocument();
var designerCanvasSize = new Size(designerCanvas.ActualWidth * zoom,
designerCanvas.ActualHeight * zoom);
var boardSize = new Size(_board.ExtentWidth, _board.ExtentHeight);
double xOffset = _board.HorizontalOffset;
double yOffset = _board.VerticalOffset;
double pageWidth = _printDialog.PrintableAreaWidth;
double pageHeight = _printDialog.PrintableAreaHeight;
double coveredX = 0;
double coveredY = 0;
double totalX = designerCanvasSize.Width;
double totalY = designerCanvasSize.Height;
int pageNumber = 1;
while (true)
{
double x = pageWidth;
double y = pageHeight;
if ((coveredX + pageWidth) > totalX)
{
x = totalX - coveredX;
}
if ((coveredY + pageHeight) > totalY)
{
y = totalY - coveredY;
}
double dpiX = 300.0;
double dpiY = 300.0;
double dpiScale = 300.0 / 96.0;
var crop = new Rect(xOffset * -1, yOffset * -1, x * dpiX, y * dpiY);
_board.Measure(boardSize);
_board.Arrange(crop);
int imgWidth = Convert.ToInt32(x * dpiScale);
int imgHeight = Convert.ToInt32(y * dpiScale);
var renderBitmap = new RenderTargetBitmap(imgWidth, imgHeight, dpiX, dpiY, PixelFormats.Pbgra32);
renderBitmap.Render(_board);
renderBitmap.Freeze();
using (var outStream = new MemoryStream())
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(outStream);
outStream.Seek(0, SeekOrigin.Begin);
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = outStream;
bitmapImage.EndInit();
var image = new Image { Source = bitmapImage };
var pageGrid = new Grid { Margin = new Thickness(0), Width = pageWidth, Height = pageHeight };
image.HorizontalAlignment = HorizontalAlignment.Left;
image.VerticalAlignment = VerticalAlignment.Top;
RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);
RenderOptions.SetEdgeMode(image, EdgeMode.Aliased);
var border = new Border
{
BorderThickness = new Thickness(1),
BorderBrush = Brushes.Transparent,
Margin = new Thickness(28),
Padding = new Thickness(2),
Child = image
};
pageGrid.Children.Add(border);
if (ShowPageNumberCheckBox.IsChecked != null && ShowPageNumberCheckBox.IsChecked == true)
{
var lblPageNumber = new Label
{
Content = string.Format("{0}", pageNumber++),
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(20),
Foreground = Brushes.Black,
};
pageGrid.Children.Add(lblPageNumber);
}
var pageContent = new PageContent();
var page = new FixedPage { Width = pageWidth, Height = pageHeight };
page.Children.Add(pageGrid);
((IAddChild)pageContent).AddChild(page);
fixedDocument.Pages.Add(pageContent);
}
xOffset += x;
coveredX += x;
if (coveredX >= totalX)
{
xOffset = _board.HorizontalOffset;
coveredX = 0;
yOffset += y;
coveredY += y;
if (coveredY >= totalY)
{
break;
}
}
Dispose();
}
_printDialog.PrintDocument(fixedDocument.DocumentPaginator, string.Format("bord_{0}", _board.Nome));
}
}
这是两种纸张尺寸的结果页: