2012-09-02 2 views
4

그래서 Im은 WriteableBitmap에 이미지를로드하려고 시도하지만 NullReferenceException이 발생합니다. 내가 생각하기 때문에이 이전writeablebitmap에 이미지로드

BitmapImage b = new BitmapImage(new Uri(@"images/Car.jpg", Urikind.Relative)); WriteableBitmap wb = new WriteableBitmap(b);

그게 전부를 작동하는 데 사용되는 이유를 알아낼 수 없습니다. 내 프로젝트의 리소스로 차 이미지가 있습니다. Image 컨트롤을 만들고 BitmapImage에 소스를 설정하여 표시 할 수 있기 때문에 제대로 작동합니다. 감사합니다

답변

11

다른 참조 질문에서 볼 수 있듯이 null 참조 예외가 나타나는 이유는 BitmapImage.CreateOptions Property 기본값은 BitmapCreateOptions.DelayCreation입니다. 당신은 BitmapCreateOptions.None로 설정하고 이미지를로드 한 후 WriteableBitmap를 만들 수 있습니다

BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative)); 
img.CreateOptions = BitmapCreateOptions.None; 
img.ImageOpened += (s, e) => 
    { 
     WriteableBitmap wbm = new WriteableBitmap((BitmapImage)s); 
    }; 

BitmapImage이로드 될 때까지 WritableBitmap가 대기하는 코드를 사용하고, 다음은 WritableBitmap에 할당 할 수 있습니다.

+0

그래, 고마워. – kiznore

+0

@kiznore 기꺼이 도와 줬어! –