2014-01-13 1 views
2

UriSrouce에서 BitmaImage를 만들고 WPF 응용 프로그램에서 인쇄해야합니다. 다음 코드를 사용하여 나는 이미지를 인쇄 할 수 있어요 :BitmapImage를 사용하여 원격 서버에서 UriSource를 설정하는 방법은 무엇입니까?

Image imgVoucher = new Image(); 
BitmapImage bImgVoucher = new BitmapImage(); 

bImgVoucher.BeginInit(); 
bImgVoucher.UriSource = new Uri(@"C:\logo-1.png", UriKind.Absolute); // Print ok 
bImgVoucher.EndInit(); 
imgVoucher.Source = bImgVoucher; 

같은 코드와 같은 이미지 만 UriSource와 웹 서버를 가리키는과 이미지가 인쇄되지 않고 오류가 발생하지 않습니다. 어떤 생각인지 내가 뭘 잘못 했니?

Image imgVoucher = new Image(); 
BitmapImage bImgVoucher = new BitmapImage(); 

bImgVoucher.BeginInit(); 
bImgVoucher.UriSource = new Uri("http://123123.com/logo.png", UriKind.Absolute); // Does not print 
bImgVoucher.EndInit(); 
imgVoucher.Source = bImgVoucher; 
+0

브라우저에서 사진을로드 할 수 있습니다. – GibboK

답변

5

이미지가 완전히 다운로드되지 않았을 수 있습니다. 인쇄하기 전에 IsDownloding 속성을 확인하고 필요한 경우 DownloadCompleted 이벤트 처리기를 추가 :

var bitmap = new BitmapImage(new Uri("http://123123.com/logo.png")); 

if (!bitmap.IsDownloading) 
{ 
    // print immediately 
} 
else 
{ 
    bitmap.DownloadCompleted += (o, e) => 
    { 
     // print when download completed 
    }; 
} 

대안 (동기) 솔루션은, 예를 들어 BitmapImage를 생성하기 전에 전체 이미지 데이터를 다운로드하는 것입니다 like :

var buffer = new WebClient().DownloadData("http://123123.com/logo.png"); 
var bitmap = new BitmapImage(); 

using (var stream = new MemoryStream(buffer)) 
{ 
    bitmap.BeginInit(); 
    bitmap.CacheOption = BitmapCacheOption.OnLoad; 
    bitmap.StreamSource = stream; 
    bitmap.EndInit(); 
} 

// print now 
+0

솔루션 설정이 동기화 될 수 있습니까? 당신의 대답에 감사드립니다. – GibboK

관련 문제