2009-05-28 6 views
0

내 C# 코드가 내 (WPF) 응용 프로그램의 캔버스에 이미지를 추가하려고합니다. 그러나, 내 코드가 작동하지 않습니다.C#/WPF 이미지 처리 문제

Image I = new Image(); 
I.Source = System.IO.File.Open(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", System.IO.FileMode.Open); 

나는 오류 얻을 : 내가 볼

Cannot implicitly convert type 'System.IO.FileStream' to 'System.Windows.Media.ImageSource' 

을하는 이유는 다음과 같습니다 이미지 객체는 원시 비트 맵 (또는 JPG 또는 무엇이든)를 원하고, 내 코드는 그것에게의 출력 스트림을주고있다 파일. 이 둘을 어떻게 변환합니까?

답변

3

약 :

Image I = new Image(); 
BitmapImage bi = new BitmapImage(); 
bi.BeginInit(); 
bi.UriSource = new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", UriKind.Absolute); 
bi.EndInit(); 
I.Source = bi; 
+0

BitmapImage는 열린 우리당을 받아들이는 생성자 오버로드가 있습니다. 귀하의 모범과 다를 것이 있습니까? – YotaXP

+0

이 작업을하려면 무엇을 가져올 필요가 있습니까? Visual Studio에서 "System.Windows.Controls.Image"에 'FromStream'에 대한 정의가 포함되어 있지 않습니다. "라고 말합니다. –

+1

죄송합니다. System.Drawing.Image와 System.Windows.Controls.Image를 혼란스럽게 만들었습니다. 오늘 아침이 게시물에 대한 개정. 나는 올바른 해결책으로 되돌아 갔다. – jason