문제
나는 어떤 종류의 통제에 이미지를 묶고 나중에 삭제하고 싶습니다.
path = @"c:\somePath\somePic.jpg"
FileInfo fi = new FileInfo(path);
Uri uri = new Uri(fi.FullName, UriKind.Absolute);
var img = new System.Windows.Controls.Image();
img.Source = new BitmapImage(uri);
이제이 코드 후에 파일을 삭제하고 싶습니다.
fi.Delete();
그러나 이미 이미지가 사용되고 있기 때문에 그렇게 할 수 없습니다. Code Fragment 1 en 2 사이에 무엇을 해제하려면 무엇을 할 수 있습니까?
해결책
ImagesOURCE에 제공하기 전에 이미지를 MemoryStream에 복사하십시오.
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 30;
bi.StreamSource = byteStream;
bi.EndInit();
여기서 Bytestream은 MemoryStream의 파일 사본입니다
또한 이것 유용 할 수 있습니다
다른 팁
당신은 a를 사용할 수 있습니다 MemoryStream
그러나 비트 맵 데이터의 두 개의 별도 사본이 RAM에 보관되어 있기 때문에 실제로 메모리를 낭비합니다. MemoryStream
하나의 사본을 만들고 비트 맵이 디코딩되면 다른 사본이 만들어집니다. 사용의 또 다른 문제 MemoryStream
이런 식으로 캐시를 우회하는 것입니다.
이를 수행하는 가장 좋은 방법은 BitMapCacheOptions.onload를 사용하여 파일에서 직접 읽는 것입니다.
path = @"c:\somePath\somePic.jpg"
var source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
source.CacheOption = BitmapCacheOption.OnLoad;
source.EndInit(); // Required for full initialization to complete at this time
var img = new System.Windows.Controls.Image { Source = source };
이 솔루션은 효율적이고 간단합니다.
참고 : 실제로 캐시를 우회하고 싶다면 이미지가 디스크에서 변경 될 수 있으므로 설정해야합니다. CreateOption = BitmapCreateOption.IgnoreImageCache
. 그러나이 경우 에도이 솔루션은 다음과 같습니다 MemoryStream
솔루션은 이미지 데이터 사본을 RAM에 보관하지 않기 때문입니다.
제휴하지 않습니다 StackOverflow