Csharp/C#教程:在WPF中将图像添加到FixedPage分享


在WPF中将图像添加到FixedPage

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

上述就是C#学习教程:在WPF中将图像添加到FixedPage分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1000179.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐