2012-08-07 2 views
1

다른 UIElements로 이미지를 인쇄하고 싶습니다. FixedPage 인스턴스가있어서 이미지를 추가하려고합니다.WPF의 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와 같은)가 아무런 문제없이 나타나기 때문에 왜 그런지 궁금합니다. 내가 뭘 놓치고 있니?

감사합니다.

추신 : 다른 해결책을 찾았습니다. 왜 그런데 우리 사진이 제대로로드 될지 모르겠다.

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

답변

3

내 코드가 작동 StreamSource와

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

에 더 명확하게하세요! 고마워요. – Semuserable

+0

참고 사항 :이 코드를 "큰"(= 많은 페이지) FixedDocument에서 실행하면 OutOfMemoryExceptions은 코드에 의해 throw되지 않습니다. 그들은 단순히 무시되고 이미지는 표시되지 않습니다. 문제는이 코드와 다릅니다. BitmapFrame bitImage = BitmapFrame.Create (new FileStream (fileName, FileMode.Open, FileAccess.Read), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); –

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

하지 내가 개인적으로 직접 비트 맵을 사용한 작업 나쁜 아이디어라고 생각하면 이것은 다음이 이미지 유형 (system.drawing의 iguess)에 복사 내가 이미지 형태로 파일을 만들 비트 맵을 사용, 작동합니다, 뭔가 아래처럼

var tempImage = new Image {Source = bitImage}; 
var imageObject = new ImageObject(tempImage, fileName); 
bitImage.StreamSource.Dispose(); 

이 tempImage를 일반 UIElement로 추가 할 수 있습니다. 도움이 되길 바랍니다.

관련 문제