繁体   English   中英

在WPF中将图像添加到FixedPage

[英]Adding Image to FixedPage in WPF

我希望能够用其他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");

更清楚我的准则

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);

首先我想建议,见下文。

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添加到你的页面。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM