Search code examples
c#image-resizingscanningimage-sizesave-image

How to scan an image and save it with normal size in c#


I want to scan a page and save it automatically. This code works well but the problem is an image that creates and then it saves is too big! it creates an image with the size of 30Mb! How can I change this code to save an image with normal size? Here is my code:
Thanks.

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

Solution

  • Try this method to convert the raw scanned data:

    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;
          // We have inverted red and blue to get the correct scanned image
          // else it is flipped up/down and right/left with bad colors
          int b = data[i];
          int g = data[i + 1];
          int r = data[i + 2];
          bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
          i += 3;
        }
      }
      return bmp;
    }
    

    So your code is now:

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