Как отсканировать изображение и сохранить его с нормальным размером в C # - PullRequest
1 голос
/ 26 сентября 2019

Я хочу отсканировать страницу и автоматически сохранить ее.Этот код работает хорошо, но проблема в том, что изображение, которое создает, а затем сохраняет, слишком велико!создает изображение размером 30 Мб!Как я могу изменить этот код, чтобы сохранить изображение с нормальным размером?Вот мой код:
Спасибо.

        private void button7_Click(object sender, EventArgs e)
    {
        try
        {
            var deviceManager = new DeviceManager();

            for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
            {
                if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
                {
                    continue;
                }
                lstListOfScanner.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
            }
        }
        catch (COMException ex)
        {
            MessageBox.Show(ex.Message);
        }

        try
        {
            var deviceManager = new DeviceManager();

            DeviceInfo AvailableScanner = null;

            for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
            {
                if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
                {
                    continue;
                }

                AvailableScanner = deviceManager.DeviceInfos[i];

                break;
            }
            var device = AvailableScanner.Connect(); //Connect to the available scanner.
            var ScanerItem = device.Items[1]; // select the scanner.

            var imgFile = (ImageFile)ScanerItem.Transfer(FormatID.wiaFormatJPEG); //Retrive an image in Jpg format and store it into a variable.
            var Path = @"C:\....\ScanImg.jpg"; // save the image in some path with filename.
            if (File.Exists(Path))
            {
                File.Delete(Path);
            }
            imgFile.SaveFile(Path);
          }
        catch (COMException ex)
        {
            MessageBox.Show(ex.Message);
        }
        /////////////////////////////////////
    }

1 Ответ

1 голос
/ 26 сентября 2019

Попробуйте этот метод для преобразования необработанных отсканированных данных:

public Bitmap GetBitmapFromRawData(int w, int h, byte[] data)
{
  Bitmap bmp = new Bitmap(w, h);
  int i = 0;
  for ( int y = 0; y < h; y++ )
  {
    for ( int x = 0; x < w; x++ )
    {
      int a = 255;
      int r = data[i];
      int g = data[i + 1];
      int b = data[i + 2];
      bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
      i += 3;
    }
  }
  return bmp;
}

Итак, ваш код теперь:

private void button1_Click(object sender, EventArgs e)
{
  try
  {
    var deviceManager = new DeviceManager();

    for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ ) // Loop Through the get List Of Devices.
    {
      if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType ) // Skip device If it is not a scanner
      {
        continue;
      }
      lstListOfScanner.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
    }
  }
  catch ( COMException ex )
  {
    MessageBox.Show(ex.Message);
  }

  try
  {
    var deviceManager = new DeviceManager();

    DeviceInfo AvailableScanner = null;

    for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ ) // Loop Through the get List Of Devices.
    {
      if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType ) // Skip device If it is not a scanner
      {
        continue;
      }

      AvailableScanner = deviceManager.DeviceInfos[i];

      break;
    }
    var device = AvailableScanner.Connect(); //Connect to the available scanner.
    var ScanerItem = device.Items[1]; // select the scanner.

    var imgFile = (ImageFile)ScanerItem.Transfer();
    var data = (byte[])imgFile.FileData.get_BinaryData();
    var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);

    var Path = @"C:\....\ScanImg.jpg"; // save the image in some path with filename.
    if ( File.Exists(Path) )
    {
      File.Delete(Path);
    }
    bitmap.Save(Path, ImageFormat.Jpeg);
  }
  catch ( COMException ex )
  {
    MessageBox.Show(ex.Message);
  }
...