Here is why:
http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.110%29.aspx[
^].
Still did not get it? Look at "is equivalent to" line.
Still did not get it? Now, let's look at the definitions:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx[
^].
As you can see,
System.Windows.Media.ImageSource
is the indirect base class of
System.Windows.Media.Imaging.BitmapImage
. It means, the
BitmapImage
is always
ImageSource
, but
ImageSource
is not always
BitmapImage
. Got it, finally? No?
Look, they are
assignable like that:
BitmapImage bitmap =
ImageSource src = bitmap;
but not in reverse direction. In reverse direction, you rightfully can use
as
. This operator returns null if unsuccessful and reference to the same object if successful, with the
compile-time type of
ImageSource
and
run-time type of
BitmapImage
. In your case, null indicated that
your object is not BitmapImage
.
Please see also my past answer:
run-time polymorphism VS compile-time polymorphism[
^].
Now, what to do? You simply should forget about
BitmapImage
, which you don't have and don't need. Any
ImageSource
can be serialized into array of bytes. Please see:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8327dd31-2db1-4daa-a81c-aff60b63fee6/[
^].
Please see this CodeProject article on TWAIN for WPF:
Twain for WPF Applications - Look Ma, No Handles[
^].
—SA