扫描后缩小图像尺寸?

时间:2014-11-04 13:07:15

标签: c# asp.net wia scanning

我有扫描文件的代码并将其保存在我的路径中:

public ImageFile Scan()
    {
        var device = this._deviceInfo.Connect();

        var item = device.Items[1];
        var imageFile = (ImageFile)item.Transfer(FormatID.wiaFormatJPEG);

        return imageFile;
    }

  protected void btnscan_Click(object sender, EventArgs e)
    {
        foreach (Object scanner in scanners)
        {
            if (ddlSelectDevice.SelectedItem.ToString().Equals(scanner.ToString()))
            {
                //var d = ddlSelectDevice.SelectedIndex as Scanner;
                var device = scanner as Scanner;
                if (device == null)
                {

                    return;
                }

                var image = device.Scan();

                var path = "~/Scanner/scan.jpeg";
                if (File.Exists(Server.MapPath(path)))
                {
                    File.Delete(Server.MapPath(path));
                }
                image.SaveFile(Server.MapPath(path));
                break;
            }
        }
    }

我通过转换为byte:

将此图像保存在数据库中
 private byte[] image2Byte()
    {
        string filePath = null;
        Byte[] bytes = null;

        if (File.Exists(Server.MapPath("~/Scanner/scan.jpeg")))
        {
            filePath = Server.MapPath("~/Scanner/scan.jpeg");
            return bytes = File.ReadAllBytes(filePath);
        }
        else
        {
            filePath = Server.MapPath("~/Scanner/None.jpg");
            return bytes = File.ReadAllBytes(filePath);
        }

    }

但是我的图像通常有大尺寸(高达400kB)并且在加载时出现问题。我如何减少图像尺寸?

1 个答案:

答案 0 :(得分:2)

您可以使用ImageProcessor来降低JPEG文件的质量。大多数用例都可以达到80%的质量。

using (ImageFactory imageFactory = new ImageFactory(preserveExifData:true))
{
    imageFactory.Load(inStream)
                .Quality(80)
                .Save(outStream);
}