Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does a WPF BitmapImage object not download an image from a Uri Source in ASP.Net Web Forms?
    primarykey
    data
    text
    <p>I'm trying to accomplish the following in ASP.Net:</p> <ol> <li>Create a WPF Canvas control</li> <li>Spin up a WPF Image control and a BitmapImage object</li> <li>Set the BitmapImage source to a Uri for an image</li> <li>Add the image to the canvas</li> <li>When the image is downloaded render the canvas to a new bitmap</li> </ol> <p>My code works correctly in WPF itself, however when running in an ASP.Net page the image is not downloaded.</p> <p>It works totally fine for other WPF UI elements. In the case of Image, using the BitmapImage.StreamSource property to set the source works correctly. When I use the BitmapImage.UriSource property the BitmapImage.DownloadCompleted event isn't raised, which hints that the image never starts downloading in the first place.</p> <p>It's important to note that it works fine for most controls - ellipses, rectangles, ink presenters, and also the Image control so long as I use a stream source rather than a uri source.</p> <p>So, what am I missing here? Why does the BitmapImage class behave differently in a web application?</p> <p>I know I'll get asked so the purpose in doing this is that I have written a Silverlight client to create graphical content which is stored on a web server. I want the web server to render the content to bitmap files.</p> <p>Thanks in advance for any advice..</p> <p>Here's my code for the ASP.Net page:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Windows.Controls; using System.Windows.Media.Imaging; using System.Threading; using System.Windows; using System.Net; using System.IO; using System.Windows.Media; public partial class _Default : System.Web.UI.Page { private static Canvas c; protected void Page_Load(object sender, EventArgs e) { Thread t = new Thread(new ThreadStart( delegate { DownloadAndSave(); })); t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join(); } [STAThread] void DownloadAndSave() { c = new Canvas(); BitmapImage bitmap = new BitmapImage(); System.Windows.Controls.Image image = new System.Windows.Controls.Image(); bitmap.DownloadCompleted += new EventHandler(bitmap_DownloadCompleted); bitmap.BeginInit(); bitmap.UriSource = new Uri("http://andrew.myhre.tanash.net/customassets/andrewmyhre/image/face.jpg"); bitmap.EndInit(); image.Source = bitmap; c.Children.Add(image); c.UpdateLayout(); c.Measure(new Size(400, 300)); c.Arrange(new Rect(new Size(400, 300))); } void bitmap_DownloadCompleted(object sender, EventArgs e) { // this never fires!! SaveImage(c); } void SaveImage(UIElement element) { RenderTargetBitmap bmp = new RenderTargetBitmap(400, 300, 96, 96, PixelFormats.Pbgra32); bmp.Render(element); BitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(Server.MapPath("~/file.jpg"))) encoder.Save(stm); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload