Использовать сжатие. Этот пример 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);
и реализация .Image есть ...
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);
}
}
}