我希望能够使用其他UIElements打印图像。我有一个FixedPage实例,并试图像这样添加图像
// Basic printing stuff
var printDialog = new PrintDialog();
var fixedDocument = new FixedDocument();
fixedDocument.DocumentPaginator.PageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
FixedPage page = new FixedPage();
page.Width = fixedDocument.DocumentPaginator.PageSize.Width;
page.Height = fixedDocument.DocumentPaginator.PageSize.Height;
Image img = new Image();
// PrintIt my project's name, Img folder
var uriImageSource = new Uri(@"/PrintIt;component/Img/Stuff.png", UriKind.RelativeOrAbsolute);
img.Source = new BitmapImage(uriImageSource);
img.Width = 100;
img.Height = 100;
page.Children.Add(img);
PageContent pageContent = new PageContent();
((IAddChild)pageContent).AddChild(page);
fixedDocument.Pages.Add(pageContent);
// Print me an image please!
_printDialog.PrintDocument(fixedDocument.DocumentPaginator, "Print");
它给了我一张白纸。我想知道为什么,因为其他UIElements(如TextBox,Grid,Rect)出现没有问题。我错过了什么?
谢谢!
PS好的,我找到了另一个解决方案。我不知道为什么但是那张Uri的图片正确加载
var uri = new Uri("pack://application:,,,/Img/Stuff.png");
答案 0 :(得分:3)
更清楚我的代码
var bitImage = new BitmapImage();
bitImage.BeginInit();
bitImage.StreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read);
bitImage.DecodePixelWidth = 250;
bitImage.CacheOption = BitmapCacheOption.OnLoad;
bitImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitImage.EndInit();
bitImage.StreamSource.Seek(0, System.IO.SeekOrigin.Begin);
bitImage.Freeze();
var tempImage = new Image {Source = bitImage};
var imageObject = new ImageObject(tempImage, fileName);
bitImage.StreamSource.Dispose();
page.Children.Add(imageObject);
答案 1 :(得分:0)
首先我想建议,见下文。
Set the sourceStream of the Image for BitMap
bitImage.StreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read);
然后冻结BitMap图像,或者它不会在实例中作为可渲染图像。
bitImage.StreamSource.Seek(0, System.IO.SeekOrigin.Begin);
bitImage.Freeze();
这应该可行,如果不是我个人认为直接使用BitMaps是一个坏主意,我使用Bitmap创建图像表单文件然后将其复制到Image(system.drawing iguess)类型,如下所示
var tempImage = new Image {Source = bitImage};
var imageObject = new ImageObject(tempImage, fileName);
bitImage.StreamSource.Dispose();
此tempImage可以作为常规UIElement添加到ur页面。 希望它有所帮助。