"Oliver Sturm [MVP C#]" <oliver@sturmnet.org> wrote in message
news:1of44e3mq4rq0$.dlg@sturmnet.org...
> Hi,
>
> Jon Davis wrote:
>
>> OK, why is Canvas not IDisposable, and how do I get rid of all the
>> Windows
>> handles?
>
> I used a variation of your code to try this myself, but I was unable to
> reproduce the crash behaviour your were referring to.
That's interesting. It might help if I mention I'm trying to do this on an
ASP.NET web page to see about the feasibility of dynamically-generating
images from XAML mark-up, on a heavily impacted web site. The output of
this is great and seems to be fast, it's just that it crashes after 500
times and IIS's process has 6000 Windows handles. I'm executing on Windows
Vista / IIS 7. Here's the full source. I'll take a look at yours as well,
but in the mean time ... maybe you can spot something really, really stupid
I'm doing?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ImageGenServer
{
public partial class GenImage : System.Web.UI.Page
{
private System.IO.Stream retImageStream;
protected void Page_Load(object sender, EventArgs e)
{
Gimme();
}
protected void Gimme()
{
System.Threading.Thread thread = new System.Threading.Thread(
GimmeMore);
thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start();
while (thread.IsAlive)
{
System.Threading.Thread.Sleep(0);
}
Response.ContentType = "image/jpeg";
System.IO.BinaryReader br = new
System.IO.BinaryReader(retImageStream);
Response.BinaryWrite(br.ReadBytes((int)retImageStream.Length));
br.Close();
retImageStream.Close();
Response.Flush();
thread = null;
br = null;
retImageStream = null;
Response.End();
}
protected void GimmeMore()
{
System.IO.StreamReader sr = new
System.IO.StreamReader(Request.PhysicalApplicationPath + "Window1.xaml");
string xaml = sr.ReadToEnd();
if (Request["text"] != null)
{
xaml = xaml.Replace("{$TEXT$}", Request["text"]);
}
else
{
xaml = xaml.Replace("{$TEXT$}", "Add "text=..." to
querystring.");
}
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();
}
}
}