SilverLight series -- Image control BUG

Source: Internet
Author: User
Tags high cpu usage

Recently, when developing silverlight, I found that as long as the program is running and debugging is enabled or not, the CPU usage of the IE process is always up or down 90%. However, it is normal when some forms are displayed, this is a big deal. When we hand it over to the customer, we can see that this CPU usage cannot be killed. After investigation by multiple parties, we found that the forms that cause high CPU usage all have one thing in common: the Image control is used, but no Image is displayed (the Image address is assigned to the Image. source attribute, but the actual part does not exist), is this the culprit?

Google posted a message and found a post:

Binding silverlight image control to an empty BitmapImage causes high cpu utilization

This is the description of two people in the post:

When a silverlight image control is bound to an empty BitmapImage, sustained high CPU utilization occurs (15-25% on my machine ). when source is set to null or to a valid image, cpu use drops to idle.

If this. ImageUrl. Value is null the SL app takes up close to 100% CPU on one core as long as it is visible on screen. Minimizing the window brings CPU down to 0%

The reply from Microsoft is as follows:

Thank you for reporting this issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

There is a problem! This is also true for Microsoft. Such a commonly used control also produces this issue. Blame is useless. I still want to solve it. I figured out a way not to directly use the Image. source value, but through some code, try to request the file stream according to the Image path, if successful, instantiate a BitmapImage assigned to the Image. source. Otherwise. set Source to null. Code:

    public static class Img    {        public static readonly DependencyProperty UriProperty;        static Img()        {            UriProperty = DependencyProperty.RegisterAttached("Uri", typeof(Uri), typeof(Img), 
                 new PropertyMetadata(null, UriPropertyChanged));        }        public static Uri GetUri(Image image)        {            return (Uri)image.GetValue(UriProperty);        }        public static void SetUri(Image image, Uri uri)        {            image.SetValue(UriProperty, uri);        }        static void UriPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)        {            Image image = sender as Image;            Uri uri = e.NewValue as Uri;            TrySetImageSourceByUri(image, uri);        }        static void TrySetImageSourceByUri(Image image, Uri uri)        {            if (string.IsNullOrEmpty(uri.OriginalString))            {                image.SetValue(Image.SourceProperty, null);                return;            }            WebClient client = new System.Net.WebClient();            client.OpenReadCompleted += (sender, e) =>            {                if (e.Error == null)                {                    BitmapImage bitImg = new BitmapImage();                    bitImg.SetSource(e.Result);                    image.Source = bitImg;                }                else                    image.SetValue(Image.SourceProperty, null);            };            client.OpenReadAsync(uri);        }    }
The usage is similar to the following:
    <Image local:Img.Uri=”Images/TestPic.jpg” />
1. The actual address of the above "Images/TestPic.jpg" is ClientBin/Images/TestPic.jpg. If the Source attribute is directly set, it must be written as "/Images/TestPic.jpg ";
2. The above method does not support XAP package addresses, similar to: "/VDP. SL. Controls; component/Images/TestPic.jpg". It is not recommended for individuals to put Images under the silverlight project,
Because the XAP package is large, the small icons used on the toolbar can be determined according to the situation;
On January 19, another possible error caused by Image. Source was found:
When I open a silverlight form, the browser prompts the following Script Error:
Message: Unhandled Error in Silverlight Application
Code: 4009
Category: ManagedRuntimeError
Message: the element is a child element of another element.
This error cannot be captured, because it cannot be found in the VS debugger, and the location of the error is completely unknown from the script error. You can only use the most stupid method to annotate the control one by one and try it over and over again. It is finally found that it is a disaster caused by the Image.
Source = "{Binding Path = [PositiveImgRUL. Of course, I will change to: vdp: Img. Uri = "{Binding Path = [PositiveImgRUL]}"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.