Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0
I scan a picture with wpfTwain. and display image into system.windows.control.image.
 
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();
        encoder.Frames.Add(BitmapFrame.Create(imageC));//error
        encoder.Save(memStream);
        return memStream.GetBuffer();
}
 

 

 
call as :
 
getJPGFromImageControl(firmaUno.Source as BitmapImage)
 
 

error as : imageC is null !!!
 
why?
 
firmaUno.Source is not null !! but after the use from (firmaUno.Source as BitmapImage) ... BitmapImage is null!!!!
 
firmaUno.Source is system.windows.control.image
Posted 9-Oct-12 4:15am
Comments
Sergey Alexandrovich Kryukov at 9-Oct-12 13:02pm
   
Why on Earth would you need BitmapImage?
--SA

1 solution

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

Solution 1

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
  Permalink  
v6
Comments
RaisKazi at 9-Oct-12 13:28pm
   
My 5.
Sergey Alexandrovich Kryukov at 9-Oct-12 13:32pm
   
Thank you, Rais.
--SA

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 CPallini 333
2 George Jonsson 270
3 OriginalGriff 159
4 Prasad Khandekar 135
0 OriginalGriff 4,613
1 CPallini 3,340
2 Sergey Alexandrovich Kryukov 2,771
3 George Jonsson 2,254
4 Gihan Liyanage 2,020


Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2014
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