Question

I have an application that uses a bitmap converter. The converter will process hundreds/thousands of images during the app's execution.

Slight twist: In the event that the converter throws an exception (for one reason or another), I need the converter to return a generic error image (a red 'x', for example) that will be displayed in place of the image itself. This will convey to the user that something went wrong. The image is packed in to the assembly as an embedded resource.

My strategy is to make a static constructor for the converter like this...

 private static readonly BitmapImage ErrorBitmapImage;
    static ImageConverter()
    {
        var assembly = Assembly.GetCallingAssembly();
        var name = assembly.GetManifestResourceNames().FirstOrDefault(q => q.Contains("MC900432546.PNG"));
        BitmapImage bi = new BitmapImage();
        bi.BeginInit();
        bi.DecodePixelWidth = 30; 
        using (var stream = assembly.GetManifestResourceStream(name))
        {
            bi.StreamSource = stream;
        }
        bi.EndInit();
        bi.Freeze();
        ErrorBitmapImage = bi;
        ErrorBitmapImage.Freeze();
    }

Observe that the constructor freezes an object twice, this raises an exception that a frozen object cannot be frozen. Otherwise the code works as expected. The two freezes are coded in here to make the question clearer, I know one must be removed.

One (or possibly both) of the calls to Freeze needs to be deleted. I have tried both options and cannot detect a difference. I wanted to get the answer, if there is one, before embarking on volume testing. Which Freeze should be deleted? Or both?

Was it helpful?

Solution

It is a good idea to Freeze the resources that don't change overtime like in your case. These can make some resources operations faster. However there is no difference between those Freeze calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top