Click here to Skip to main content
13,291,084 members (58,669 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I scan a picture with wpfTwain. and display image into

How do I convert the image to binary?

I used the following method

public byte[] getJPGFromImageControl(BitmapImage imageC)
       MemoryStream memStream = new MemoryStream();              
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        return memStream.GetBuffer();

call as :

getJPGFromImageControl(firmaUno.Source as BitmapImage)

error as : imageC is null !!!


firmaUno.Source is not null !! but after the use from (firmaUno.Source as BitmapImage) ... BitmapImage is null!!!!

firmaUno.Source is
Posted 9-Oct-12 5:15am
Why on Earth would you need BitmapImage?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is why:[^].

Still did not get it? Look at "is equivalent to" line.

Still did not get it? Now, let's look at the definitions:[^].

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:[^].

Please see this CodeProject article on TWAIN for WPF: Twain for WPF Applications - Look Ma, No Handles[^].

RaisKazi 9-Oct-12 13:28pm
My 5.
Thank you, Rais.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100