2012-01-19 62 views
2

我使用WIA 2.0掃描HP掃描儀的圖像。問題是保存的TIFF大約9MBs(A4紙,300dpi,灰度)。我轉換包含在TIFF格式掃描WIA的鏡像文件到的BitmapSource這樣的:WIA掃描WPF應用程序

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,你可以預覽和旋轉)?謝謝。

+0

BTW沒有必要關閉使用塊內的流,因爲處理期間流自動關閉。 – Clemens

回答

3

使用壓縮。這個例子Ccitt4用於黑白傳真壓縮,壓縮因數很大,但如果你想保持灰度級,還有其他版本。

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); 

圖像配和的實施是...

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); 
     } 
    } 
}