在WPF将图片添加到固定页面 [英] Adding Image to FixedPage in WPF

查看:196
本文介绍了在WPF将图片添加到固定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够与其他UI元素打印图像。我有一个固定页面实例,并尝试添加图像像这样

  //基本打印的东西
变种PrintDialog类=新PrintDialog类();
VAR固定文档=新的固定文档();
fixedDocument.DocumentPaginator.PageSize =新的大小(printDialog.PrintableAreaWidth,printDialog.PrintableAreaHeight);

固定页面页=新固定页面();
page.Width = fixedDocument.DocumentPaginator.PageSize.Width;
page.Height = fixedDocument.DocumentPaginator.PageSize.Height;

图片IMG =新的图像();

//为printit我的项目的名称,图文件夹
变种uriImageSource =新的URI(@/为printit;组件/图/ Stuff.png,UriKind.RelativeOrAbsolute);
img.Source =新的BitmapImage(uriImageSource);
img.Width = 100;
img.Height = 100;

page.Children.Add(IMG);

PageContent pageContent =新PageContent();
((IAddChild)pageContent).AddChild(页);

fixedDocument.Pages.Add(pageContent);

//我打印的图像,请!
_printDialog.PrintDocument(fixedDocument.DocumentPaginator,打印);



它给了我一张白纸。我不知道为什么,因为其他UI元素(如文本框,网​​格,矩形)出现,没有任何问题。我缺少什么?



谢谢!



PS好了,我发现了另一个解决方案。我不知道为什么,但与URI的图片正确加载

  VAR URI =新的URI(包://应用:,,, /图/ Stuff.png); 


解决方案

要在我的代码

更清晰

  VAR bitImage =新的BitmapImage(); 
bitImage.BeginInit();
bitImage.StreamSource =新的FileStream(文件名,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();

变种tempImage =新的图像{源= bitImage};
变种imageObject =新ImageObject(tempImage,文件名);
bitImage.StreamSource.Dispose();
page.Children.Add(imageObject);


I want to be able to print images with other UIElements. I have a FixedPage instance and trying to add image like so

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

It gives me a blank paper. I'm wondering why, because other UIElements (such as TextBox, Grid, Rect) appear with no problems. What am I missing?

Thanks!

PS OK, I've found another solution. I don't know why but with that Uri a picture loads properly

var uri = new Uri("pack://application:,,,/Img/Stuff.png");

解决方案

To be more clear on my Code

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

这篇关于在WPF将图片添加到固定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆