WIA扫描在WPF应用程序 [英] WIA scanning in a WPF application

查看:223
本文介绍了WIA扫描在WPF应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是2.0 WIA扫描从HP扫描仪图像。问题是,保存的TIFF格式都是围绕9MBs大(在300dpi的A4页面,灰度)。我转换WIA的镜像文件,其中包含TIFF格式扫描到的BitmapSource是这样的:

I'm using WIA 2.0 to scan images from a HP scanner. The problem is that saved TIFFs are around 9MBs big (A4 page at 300dpi, grayscale). I convert WIA's ImageFile that contains scan in TIFF format to BitmapSource like this:

    public static BitmapSource ConvertScannedImage(ImageFile imageFile)
    {
        if (imageFile == null)
            return null;

        // save the image out to a temp file
        string fileName = Path.GetTempFileName();

        // this is pretty hokey, but since SaveFile won't overwrite, we
        // need to do something to both guarantee a unique name and
        // also allow SaveFile to write the file
        File.Delete(fileName);

        // now save using the same filename
        imageFile.SaveFile(fileName);

        BitmapFrame img;

        // load the file back in to a WPF type, this is just
        // to get around size issues with large scans
        using (FileStream stream = File.OpenRead(fileName))
        {
            img = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

            stream.Close();
        }

        // clean up
        File.Delete(fileName);

        return img;
    }



任何人有一个想法如何缩小图像尺寸,如果可能的话在存储器(因为我有,你可以预览并旋转扫描的lsit)?谢谢你。

Anybody have an idea how to reduce image size, if possible in-memory (because I have a lsit of scans that you can preview and rotate)? Thanks.

推荐答案

使用压缩。这个例子CCITT4是黑白传真压缩,压缩因子是巨大的,但也有其他的版本,如果你想保持灰度。

Use compression. This example Ccitt4 is for black and white fax compression, the compression factor is huge but there are other versions if you want to keep the grey scale.

using System.Windows.Media.Imaging;


public static byte[] ConvertBitmapSourceToByteArray(BitmapSource imageToConvert, ImageFormat formatOfImage)
{
    byte[] buffer;
    try
    {
        using (var ms = new MemoryStream())
        {
            switch (formatOfImage)
            {
                case ImageFormat.Png:
                    var bencoder = new PngBitmapEncoder();
                    bencoder.Frames.Add(BitmapFrame.Create(imageToConvert));
                    bencoder.Save(ms);
                    break;

                case ImageFormat.Tiff:
                    var tencoder = new TiffBitmapEncoder();
                    tencoder.Compression = TiffCompressOption.Ccitt4;
                    tencoder.Frames.Add(BitmapFrame.Create(imageToConvert));
                    tencoder.Save(ms);
                    break;
            }
            ms.Flush();
            buffer = ms.GetBuffer();
        }
    }
    catch (Exception) { throw; }

    return buffer;
}

然后将映像

doc.SaveDirectory = DestinationDirectoryImages;
doc.Filename = fName;
doc.Image = ImageConversion.ConvertBitmapSourceToByteArray(img.Image, ImageFormat.Tiff);

和在图像配的实施...

and the implementation of .Image is...

private byte[] _image;
/// <summary>
/// Bytes for Image. Set to null to delete related file.
/// </summary>
public virtual byte[] Image
{
    get
    {
        if (_image == null)
        {
            if (SaveDirectory == null) throw new ValidationException("SaveDirectory not set for DriverDoc");
            string fullFilename = Path.Combine(SaveDirectory, Filename);
            if (!string.IsNullOrEmpty(fullFilename))
                if (File.Exists(fullFilename))
                    _image = File.ReadAllBytes(fullFilename);
                else
                    _image = File.ReadAllBytes("Resources\\FileNotFound.bmp");
        }
        return _image;
    }
    set
    {
        if (_image == value) return;
        _image = value;
        if (SaveDirectory == null) throw new ValidationException("SaveDirectory not set for DriverDoc");
        string fullFilename = Path.Combine(SaveDirectory, Filename);
        if (_image != null)
        {
            if (!string.IsNullOrEmpty(fullFilename))
            {
                _image = value;
                File.WriteAllBytes(fullFilename, _image);
            }
        }
        else
        {
            if (!string.IsNullOrEmpty(Filename) && File.Exists(fullFilename))
                File.Delete(fullFilename);
        }
    }
}

这篇关于WIA扫描在WPF应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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