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;
}
- Is the call to System.Drawing.Bitmap constructor lazy?
- Is the call to .Size lazy?
- Is the BitmapImage constructor lazy?
- Is there any other way that I can implement it to be fully lazy?
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.