扫描图像并转换为byte [] [英] scan image and convert to byte[]

查看:93
本文介绍了扫描图像并转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用wpfTwain扫描图片.并将图像显示到system.windows.control.image.

如何将图像转换为二进制?

我使用了以下方法

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)




错误如:imageC为空!!!

为什么?

firmaUno.Source不是null!但是从(firmaUno.Source作为BitmapImage)使用之后... BitmapImage为null !!!!

firmaUno.Source是system.windows.control.image




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

推荐答案

原因如下:http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.110%29.aspx [ ^ ].

还是没有得到?看看等于"行.

还是没有得到?现在,让我们看一下定义:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx [ ^ ].

如您所见,System.Windows.Media.ImageSourceSystem.Windows.Media.Imaging.BitmapImage的间接基类.这意味着BitmapImage始终为ImageSource,但ImageSource并非始终为BitmapImage.知道了吗?不是吗?

看,它们是 可分配的 ,就像这样:
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;

但不是反方向.相反,您可以正确使用as.如果运算符失败,则该操作符返回null;如果成功,则返回对同一对象的引用,其编译时间类型为ImageSource,而运行时类型为BitmapImage.在您的情况下,null表示您的对象不是 BitmapImage.

另请参阅我过去的答案:运行时多态VS编译时多态 [ http://social.msdn.microsoft.com /forums/zh-CN/wpf/thread/8327dd31-2db1-4daa-a81c-aff60b63fee6/ [ Twain对于WPF应用程序-看起来没手感 [

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


这篇关于扫描图像并转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆