首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WPF中向FixedPage添加图像

在WPF中向FixedPage添加图像
EN

Stack Overflow用户
提问于 2012-08-08 03:54:26
回答 2查看 4.7K关注 0票数 1

我想能够打印图像与其他UIElements。我有一个FixedPage实例,并尝试像这样添加图像

代码语言:javascript
复制
// 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)出现时没有任何问题。我遗漏了什么?

谢谢!

好的,我找到了另一个解决方案。我不知道为什么,但是有了这个Uri,图片就可以正常加载了

代码语言:javascript
复制
var uri = new Uri("pack://application:,,,/Img/Stuff.png");
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-08 04:17:01

更清楚地了解我的代码

代码语言:javascript
复制
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);
票数 2
EN

Stack Overflow用户

发布于 2012-08-08 04:13:33

首先,我想建议一下,如下所示。

代码语言:javascript
复制
Set the sourceStream of the Image for BitMap
    bitImage.StreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read);

然后冻结BitMap图像,否则它将不会作为可渲染图像出现在实例中。

代码语言:javascript
复制
bitImage.StreamSource.Seek(0, System.IO.SeekOrigin.Begin);
bitImage.Freeze();

这应该可以工作,如果不是我个人认为这是一个坏主意,直接与BitMaps工作,我使用位图创建图像表单文件,然后将其复制到类型图像(system.drawing我猜),类似如下

代码语言:javascript
复制
var tempImage = new Image {Source = bitImage};
var imageObject = new ImageObject(tempImage, fileName);
bitImage.StreamSource.Dispose();

此tempImage可以作为常规UIElement添加到您的页面。希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11853096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档