Reputation: 20915
I want to write a function that will get as input string fileName and return an ImageDrawing object.
I don't want to load the Bitmap from the disk in this function. Instead, I want to have some kind of lazy evaluation.
In order to find out the dimensions, I use the Bitmap class.
Currently I have this code:
public static ImageDrawing LoadImage(string fileName)
{
System.Drawing.Bitmap b = new System.Drawing.Bitmap(fileName);
System.Drawing.Size s = b.Size ;
System.Windows.Media.ImageDrawing im = new System.Windows.Media.ImageDrawing();
im.Rect = new System.Windows.Rect(0, 0, s.Width, s.Height);
im.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(fileName, UriKind.Absolute));
return im;
}
Edit:
There are many good answers that might be helpful to the community - to use Lazy class and to open it with a Task.
Nevertheless, I want to put this ImageDrawing inside a DrawingGroup and serialize afterwards, so Lazy as well as Task is not an option for me.
Upvotes: 2
Views: 909
Reputation: 689
The constructor of the Bitmap
class is not lazy but you can use the Lazy<T>
class which is made for exactly that purpose:
public static Lazy<ImageDrawing> LoadImage(string fileName)
{
return new Lazy<ImageDrawing>(() => {
System.Drawing.Bitmap b = new System.Drawing.Bitmap(fileName);
System.Drawing.Size s = b.Size;
System.Windows.Media.ImageDrawing im = new System.Windows.Media.ImageDrawing();
im.Rect = new System.Windows.Rect(0, 0, s.Width, s.Height);
im.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(fileName, UriKind.Absolute));
return im;
});
}
From the documentation (http://msdn.microsoft.com/en-us/library/dd997286.aspx):
Although you can write your own code to perform lazy initialization, we recommend that you use Lazy instead. Lazy and its related types also support thread-safety and provide a consistent exception propagation policy.
Upvotes: 4