-
WPF BitmapImage 에서 ArgumentExceptionC#/WPF 2019. 10. 14. 11:25
WPF에서 Canvas의 Background 등 ImageBrush를 사용 중 ArgumentException이 발생하면서
"값이 예상범위를 벗어났습니다." or "does not fall within the expected range" 라는 Exception을 발생할 때가 있습니다.
이는 ImageBrush만의 문제가 아니라 BitmapImage를 사용할 때 발생할 수 있습니다. (아래에 참조 링크)
BitmapImage는 한 객체당 한 Image만 참조하는 것이 기대되어 있습니다.
그래서 ImageBrush의 ImageSource를 바꾼다던지 하는 방식으로 한 객체에 여러 Image를 할당하려 할 때 발생하게 됩니다.
해법은 그냥 ImageBrush나 ImageSource를 사용하는 해당 객체를 새로 만들면 됩니다.
아래와 같은 코드를
(canvas.Background as ImageBrush).ImageSource = bitmap;
아래와 같이 바꿔주시면 됩니다.
canvas.Background = new ImageBrush(bitmap);
re-using a BitmapImage in multiple ImageBrushes results in "Value does not fall within the expected range"
Hey, in my case I'd like to have only BitmapImages loaded and reference it several dozen times. Basically, I'd like to have a few dozen ImageBrush's each pointing to the same BitmapImage to keep RAM requirements down as far as possible. I'd like to have
social.msdn.microsoft.com
'C# > WPF' 카테고리의 다른 글
WPF 타이틀 바 바꾸기(Title Bar Custom) (0) 2018.04.27 WPF 최소화 최대화 종료 버튼 만들기 (0) 2018.04.27 WPF WindowStyle None 최대화 시 작업 표시줄 보이게 하기 (0) 2018.04.26 WPF System Menu 불러오기 (0) 2018.04.26 WPF Drag, Drop이 되는 ListBox (0) 2018.02.27