Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 208879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:51:55+00:00 2026-05-11T17:51:55+00:00

I’m trying to accomplish the following in ASP.Net: Create a WPF Canvas control Spin

  • 0

I’m trying to accomplish the following in ASP.Net:

  1. Create a WPF Canvas control
  2. Spin up a WPF Image control and a BitmapImage object
  3. Set the BitmapImage source to a Uri for an image
  4. Add the image to the canvas
  5. When the image is downloaded render the canvas to a new bitmap

My code works correctly in WPF itself, however when running in an ASP.Net page the image is not downloaded.

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.

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.

So, what am I missing here? Why does the BitmapImage class behave differently in a web
application?

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.

Thanks in advance for any advice..

Here’s my code for the ASP.Net page:

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);
    }
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:51:55+00:00Added an answer on May 11, 2026 at 5:51 pm

    I think the DownloadCompleted event is never raised because BitmapImage loads asynchronously when you use the UriSource property which allows the background thread (DownloadAndSave method) to complete before the image is loaded.

    What worked for me recently in an almost identical situation was to download the bitmap resource synchronously and set it with the BitmapImage.StreamSource property. Note when using that technique that you can’t close/dispose the stream until after you’ve rendered the RenderTargetBitmap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.