나는 OutOfMemoryException
과 다시 싸울 수 있습니다.WriteableBitmapEx를 사용하여 TextBlock을 렌더링 할 때 OutOfMemoryException이 발생합니다.
Windows Phone 백그라운드 에이전트 작업 (메모리 사용량이 10M에 가까워짐)에서 WriteableBitmapEx을 사용하여 일부 이미지를 렌더링하는 코드 스 니펫이 있습니다.
다음 작품 잘 : I 추가하거나 루프에 TextBlock
를 사용하여 변경하는 경우
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var wbBG = BitmapFactory.New(0, 0);
var bmp = BitmapFactory.New(0, 0);
for (int i = 0; i < 30; i++)
{
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
wbBG = BitmapFactory.New(0, 0).FromContent("Assets/image" + i + ".jpg");
wbBG.Invalidate();
for (int j = 0; j < 6; j++)
{
bmp = BitmapFactory.New(0, 0).FromContent("Assets/" + j + ".png");
bmp = bmp.Resize(60, 60, WriteableBitmapExtensions.Interpolation.Bilinear);
wbBG.Blit(new Rect(j * 65, 0, 60, 60), bmp, new Rect(0, 0, 60, 60));
wbBG.Invalidate();
}
string filenameBG = "/Shared/" + i + ".jpg";
using (var stream = iso.CreateFile(filenameBG))
{
wbBG.SaveJpeg(stream, 480, 800, 0, 85);
stream.Close();
}
wbBG = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
NotifyComplete();
});
그러나, 그것은 OutOfMemoryException
와 제 2 루프에 실패합니다
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var wbBG = BitmapFactory.New(0, 0);
TextBlock tb;
for (int i = 0; i < 30; i++)
{
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
wbBG = BitmapFactory.New(0, 0).FromContent("Assets/image" + i + ".jpg");
//The above line would thrown OutOfMemoryException in the 2nd loop
wbBG.Invalidate();
for (int j = 0; j < 6; j++)
{
tb = new TextBlock(){
Text = j.ToString(),
//FontSize = 13,
//Height = 20,
//Width = 240,
//FontWeight = System.Windows.FontWeights.Bold,
//HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
//Foreground = new SolidColorBrush(Colors.White)
};
wbBG.Render(tb, new TranslateTransform() { X = j*65, Y = 350 });
wbBG.Invalidate();
tb = null;
}
string filenameBG = "/Shared/" + i + ".jpg";
using (var stream = iso.CreateFile(filenameBG))
{
wbBG.SaveJpeg(stream, 480, 800, 0, 85);
stream.Close();
}
wbBG = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
NotifyComplete();
});
하나를 아이디어 TextBlock
더 많은 메모리 사용량을 일으킬 것이라고 생각?
또한 이미지에 텍스트를 렌더링하는 데 더 좋은 방법이 있다는 것을 알지 못합니다.
또한 TextBlock
은 IDisposable
이 아닙니다.
글쎄, 그건 내 의견이야, 아마 틀렸어. 고마워, 고마워!
입니까? –
당신은 당신이 30 개의 이미지를 렌더링하려하고 180 개의 렌더 (Render) 호출을한다는 것을 알고 있습니까? 또한 내부 루프에서 컨트롤을 만들고 * 6 호출을 줄이는 것이 어떻습니까? 또한 첫 번째 루프 전에 이미지를 한 번로드하면 + 30 번로드하지 않습니다. –
그러면 첫 번째 작품은 왜 작동 했습니까? BTW, 각 루프마다 다른 이미지를로드해야합니다. –