Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using WIA;
  5. namespace Skaner
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         DeviceManager deviceManager = new DeviceManager();
  10.         DeviceInfo firstDevice = null;
  11.         WIA.Device device;
  12.  
  13.         int resolution = 150;
  14.         int width_pixel = 1250; //domyslnie 1250
  15.         int height_pixel = 1700; //domyslnie 1700
  16.         int jasnosc = 0;
  17.         int kontrast = 0;
  18.         int color_mode = 1;  // 1 - Kolor, 2 - Szary, 4 - Czarno-Biały
  19.         ImageFile imageFile;
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void Form1_Load(object sender, EventArgs e)
  27.         {
  28.             for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
  29.             {
  30.                 if (deviceManager.DeviceInfos[i].Type == WiaDeviceType.ScannerDeviceType)
  31.                 {
  32.                     listBoxList.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
  33.                     firstDevice = deviceManager.DeviceInfos[i];
  34.                 }
  35.             }
  36.             //listBoxList.SelectedIndex = 0;
  37.            //listBoxList.SetSelected(0, true);
  38.         }
  39.  
  40.         private void buttonStart_Click(object sender, EventArgs e)
  41.         {
  42.             var device = firstDevice.Connect();
  43.             var scannerItem = device.Items[1];
  44.             AdjustScannerSettings(scannerItem, resolution, 0, 0, width_pixel, height_pixel, jasnosc, kontrast, color_mode);
  45.             imageFile = (ImageFile)scannerItem.Transfer(FormatID.wiaFormatJPEG);
  46.  
  47.  
  48.             string path = @"C:\Users\Marcin\Desktop\Studia\Semestr 5\UP\Skaner\UPSkaner\scan.jpeg"; ///zmienic sciezke
  49.  
  50.             if (File.Exists(path))
  51.             {
  52.                 File.Delete(path);
  53.             }
  54.  
  55.             imageFile.SaveFile(path);
  56.             MessageBox.Show("Obraz został zapisany do pliku.");
  57.             pictureBox1.ImageLocation = path;
  58.  
  59.         }
  60.  
  61.         private void trackBarJasnosc_Scroll(object sender, EventArgs e)
  62.         {
  63.             jasnosc = trackBarJasnosc.Value;
  64.             labelJasnoscValue.Text = trackBarJasnosc.Value.ToString();
  65.         }
  66.  
  67.         private void trackBarKontrast_Scroll(object sender, EventArgs e)
  68.         {
  69.             kontrast = trackBarKontrast.Value;
  70.             labelKontrastValue.Text = trackBarKontrast.Value.ToString();
  71.         }
  72.  
  73.         private static void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel, int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents, int colorMode)
  74.         {
  75.             const string WIA_SCAN_COLOR_MODE = "6146";
  76.             const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147";
  77.             const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";
  78.             const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
  79.             const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
  80.             const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
  81.             const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
  82.             const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
  83.             const string WIA_SCAN_CONTRAST_PERCENTS = "6155";
  84.             SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
  85.             SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
  86.             SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
  87.             SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);
  88.             SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, scanWidthPixels);
  89.             SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, scanHeightPixels);
  90.             SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents);
  91.             SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents);
  92.             SetWIAProperty(scannnerItem.Properties, WIA_SCAN_COLOR_MODE, colorMode);
  93.         }
  94.  
  95.         private static void SetWIAProperty(IProperties properties, object propName, object propValue)
  96.         {
  97.             Property prop = properties.get_Item(ref propName);
  98.             prop.set_Value(ref propValue);
  99.         }
  100.  
  101.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  102.         {
  103.             string tryb = comboBox1.Text;
  104.  
  105.             if (tryb.Equals("Kolorowy")) color_mode = 1;
  106.             if (tryb.Equals("Czarno-bialy")) color_mode = 4;
  107.             if (tryb.Equals("Szary")) color_mode = 2;
  108.  
  109.         }
  110.  
  111.         private void pictureBox1_Click(object sender, EventArgs e)
  112.         {
  113.  
  114.         }
  115.  
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement