一次性帆布? [英] Disposable Canvas?

查看:91
本文介绍了一次性帆布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,为什么Canvas不能IDisposable,我如何摆脱所有Windows

句柄?


我正在表演循环通过动态XAML到JPEG

转换的测试。它会进行大约500次转换,然后崩溃。任务管理器

表示创建了大约6000个Windows句柄,并且计数永远不会减少



我的代码(循环未显示):


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 $}" ;,文字

}

其他

{

xaml = xaml.Replace(" {$ 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.Bi tmapFrame.Create(rtb));

string fp = Request.PhysicalApplicationPath +" imgtmp\\& quot;

+(Guid.NewGuid())。ToString()+" .jpg";

retImageStream = new System.IO.MemoryStream();

encoder.Sav e(retImageStream);

retImageStream.Seek(0,System.IO.SeekOrigin.Begin);

ms.Dispose();

}


Jon

解决方案

TEXT


}",text

}

else

{

xaml = xaml.Replace(" {

TEXT

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.Bi tmapFrame.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

解决方案

TEXT


}", text
}
else
{
xaml = xaml.Replace("{


TEXT


这篇关于一次性帆布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆