J
Jon Davis
OK, why is Canvas not IDisposable, and how do I get rid of all the Windows
handles?
I'm doing a performance test of looping through a dynamic XAML-to-JPEG
conversion. It gets to about 500 conversions and then crashes. Task Manager
says that about 6000 Windows handles were created and the count never
decrements.
My code (loop not shown):
protected System.IO.Stream GenImageStream()
{
System.IO.Stream retImageStream = null;
System.IO.StreamReader sr = new
System.IO.StreamReader("Window1.xaml");
string xaml = sr.ReadToEnd();
if (text != null)
{
xaml = xaml.Replace("{$TEXT$}", text
}
else
{
xaml = xaml.Replace("{$TEXT$}", "Enter text.");
}
sr.Close();
System.IO.MemoryStream ms = new
System.IO.MemoryStream(xaml.Length);
System.IO.StreamWriter sw = new System.IO.StreamWriter(ms);
sw.Write(xaml);
sw.Flush();
ms.Seek(0, System.IO.SeekOrigin.Begin);
System.Windows.Controls.Canvas canvas =
(System.Windows.Controls.Canvas)
System.Windows.Markup.XamlReader.Load(ms);
canvas.Background = System.Windows.Media.Brushes.Yellow;
canvas.Measure(new System.Windows.Size(640d, 480d));
canvas.Arrange(new System.Windows.Rect(0d, 0d, 640d, 480d));
System.Windows.Media.Imaging.RenderTargetBitmap rtb
= new System.Windows.Media.Imaging.RenderTargetBitmap(
640, 480, 96d, 96d,
System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);
System.Windows.Media.Imaging.JpegBitmapEncoder encoder
= new System.Windows.Media.Imaging.JpegBitmapEncoder();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));
string fp = Request.PhysicalApplicationPath + "imgtmp\\"
+ (Guid.NewGuid()).ToString() + ".jpg";
retImageStream = new System.IO.MemoryStream();
encoder.Save(retImageStream);
retImageStream.Seek(0, System.IO.SeekOrigin.Begin);
ms.Dispose();
}
Jon
handles?
I'm doing a performance test of looping through a dynamic XAML-to-JPEG
conversion. It gets to about 500 conversions and then crashes. Task Manager
says that about 6000 Windows handles were created and the count never
decrements.
My code (loop not shown):
protected System.IO.Stream GenImageStream()
{
System.IO.Stream retImageStream = null;
System.IO.StreamReader sr = new
System.IO.StreamReader("Window1.xaml");
string xaml = sr.ReadToEnd();
if (text != null)
{
xaml = xaml.Replace("{$TEXT$}", text
}
else
{
xaml = xaml.Replace("{$TEXT$}", "Enter text.");
}
sr.Close();
System.IO.MemoryStream ms = new
System.IO.MemoryStream(xaml.Length);
System.IO.StreamWriter sw = new System.IO.StreamWriter(ms);
sw.Write(xaml);
sw.Flush();
ms.Seek(0, System.IO.SeekOrigin.Begin);
System.Windows.Controls.Canvas canvas =
(System.Windows.Controls.Canvas)
System.Windows.Markup.XamlReader.Load(ms);
canvas.Background = System.Windows.Media.Brushes.Yellow;
canvas.Measure(new System.Windows.Size(640d, 480d));
canvas.Arrange(new System.Windows.Rect(0d, 0d, 640d, 480d));
System.Windows.Media.Imaging.RenderTargetBitmap rtb
= new System.Windows.Media.Imaging.RenderTargetBitmap(
640, 480, 96d, 96d,
System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);
System.Windows.Media.Imaging.JpegBitmapEncoder encoder
= new System.Windows.Media.Imaging.JpegBitmapEncoder();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));
string fp = Request.PhysicalApplicationPath + "imgtmp\\"
+ (Guid.NewGuid()).ToString() + ".jpg";
retImageStream = new System.IO.MemoryStream();
encoder.Save(retImageStream);
retImageStream.Seek(0, System.IO.SeekOrigin.Begin);
ms.Dispose();
}
Jon