why use BeginInit() with simple BitmapImages?

I have seen a lot of these on the web:

var b = new BitmapImage();
b.BeginInit();
b.UriSource = new Uri(myPath, UriKind.RelativeOrAbsolute);
b.EndInit();
myImage.Source = b;

now, as far as I'm concerned, I would first have gone for the more compact version:

myImage.Source = new BitmapImage(new Uri(myPath, UriKind.RelativeOrAbsolute));

is there any reason why I should write the first one instead of the second one?

what is it exactly that "BeginInit()" and "EndInit()" do in this case, that would not be done in the second version?

I'm guessing "nothing", but then again, I've had enough experience showing me that WPF is a lot more subtle than I would have thought it is to not wonder...

edit: Just to be clear, my point is NOT that I absolutely want to spare 4 lines of code. I'd rather like to know what those two methods do, exactly, and the reason they should (or should not) be called.

Answers


The fact that the two methods can only be used in pairs and throw an InvalidOperationException tells me that they stall the initialization of the BitmapImage. Any property initialization between the two methods will be made before the BitmapImage is initialized.

And as I said in the comment,

I guess there is no problem here because you have a handy constructor which takes the Uri as a parameter.


I think they do this, just to be consistent with other code. BitmapImage have several properties, that have to be set only once before it load the image. For example DecodePixelHeight and DecodePixelWidth. In order to set them correctly, you have to use BeginInit way. And of course, when someone shows examples, they just use the same syntax they are used to, and only remove lines of unrelated properties. If you don't need to set any of them, you may brief syntax.


Need Your Help

mysql errorno 150

mysql sql foreign-keys

I have two tables 'member' and 'book' defined as:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.