private void rotR_Click(object sender, RoutedEventArgs e)
        {
            var img = System.Drawing.Image.FromFile(savePathTxt.Text);

            img.RotateFlip(RotateFlipType.Rotate90FlipNone);
            ScanView.Source = ImageProc.ImgToBmpSource(img);
            saveImageAsFile(img);
        }
        public void InitScan()
        {
            ImageProc         imgP = new ImageProc();
            CommonDialogClass commonDialogClass = new CommonDialogClass();

            DeviceIdCmb.SelectedIndex = DeviceCmb.SelectedIndex;
            try
            {
                if (DeviceCmb.Items.Count == 0)
                {
                    MessageBox.Show("Please connect a scanner.");
                }
                Deskew _deskew = new Deskew();

                scanSettings _scanSettings = new scanSettings();

                List <System.Drawing.Image> images = null;
                images = WIAScanner.AutoScan((string)DeviceIdCmb.SelectedItem, Convert.ToInt32(dpiTxt.Text), (Convert.ToDouble(cropxTxt.Text)), (Convert.ToDouble(cropyTxt.Text)),
                                             (Convert.ToDouble(widthTxt.Text) * Convert.ToDouble(dpiTxt.Text)), (Convert.ToDouble(heightTxt.Text) * Convert.ToDouble(dpiTxt.Text)), (int)parseSettings("Brightness"),
                                             (int)parseSettings("Contrast"), (int)parseSettings("Color Mode"));
                selRect.Visibility = Visibility.Collapsed;
                foreach (System.Drawing.Image image in images)
                {
                    if ((int)parseSettings("Bpp") == 1)
                    {
                        ScanView.Source = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.BlackWhite);
                        saveImageAsFile(ImageProc.setPixelFormat2(ImageProc.ImgToBmpSource(image), System.Drawing.Imaging.PixelFormat.Format1bppIndexed));
                    }
                    if ((int)parseSettings("Bpp") == 8)
                    {
                        BitmapSource img8bit = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.Gray8);
                        ScanView.Source = img8bit;
                        saveImageAsFile(ImageProc.BmpSource2Img(img8bit));
                    }
                    if ((int)parseSettings("Bpp") == 24)
                    {
                        ScanView.Source = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.Bgr24);
                        saveImageAsFile(ImageProc.setPixelFormat2(ImageProc.ImgToBmpSource(image), System.Drawing.Imaging.PixelFormat.Format24bppRgb));
                    }
                    if ((int)parseSettings("Bpp") == 32)
                    {
                        ScanView.Source = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.Bgr32);
                        saveImageAsFile(ImageProc.setPixelFormat2(ImageProc.ImgToBmpSource(image), System.Drawing.Imaging.PixelFormat.Format32bppRgb));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Unexpected Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void previewScan()
        {
            CommonDialogClass commonDialogClass = new CommonDialogClass();

            DeviceIdCmb.SelectedIndex = DeviceCmb.SelectedIndex;
            try
            {
                if (DeviceCmb.Items.Count == 0)
                {
                    MessageBox.Show("Please connect a scanner.");
                }

                List <System.Drawing.Image> images = WIAScanner.preScan((string)DeviceIdCmb.SelectedItem);
                foreach (System.Drawing.Image img in images)
                {
                    //ScanView.Source = ImgToBmpSource(img);

                    /*Bitmap bmp = new Bitmap(img);
                     * Grayscale gs_fil = new Grayscale(0.2125, 0.7154, 0.0721);
                     * Bitmap gs_Bmp = gs_fil.Apply(bmp);
                     * DocumentSkewChecker skewCheck = new DocumentSkewChecker();
                     * double angle = skewCheck.GetSkewAngle(gs_Bmp);
                     * RotateBilinear rotateFilter = new RotateBilinear(-angle);
                     * rotateFilter.FillColor = System.Drawing.Color.White;
                     * Bitmap rotatedImage = rotateFilter.Apply(gs_Bmp);
                     * new ContrastStretch().ApplyInPlace(gs_Bmp);
                     * new Threshold(180).ApplyInPlace(gs_Bmp);
                     * //new Invert().ApplyInPlace(gs_Bmp);
                     * BlobCounter bc = new BlobCounter();
                     *
                     * bc.FilterBlobs = true;
                     * bc.ProcessImage(gs_Bmp);
                     * System.Drawing.Rectangle[] rects = bc.GetObjectsRectangles();*/
                    PreView.Source = ImageProc.ImgToBmpSource(img);
                    //gs_Bmp.Save(userProfile + "\\Pictures\\TreshSample1.png", ImageFormat.Png);

                    /* foreach (System.Drawing.Rectangle rect in rects)
                     * {
                     *   cropxTxt.Text = (rect.Left).ToString();
                     *   cropyTxt.Text = (rect.Top / 5).ToString();
                     *   heightTxt.Text = (rect.Bottom).ToString();
                     *   widthTxt.Text = (rect.Right / 45).ToString();
                     * }*/
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Unexpected Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void saveImageAsFile(System.Drawing.Image img)
        {
            string extension = readExtension(savePathTxt.Text);

            switch (extension)
            {
            case ".jpeg":
                var JPEGenc = new JpegBitmapEncoder();
                JPEGenc.Frames.Add(BitmapFrame.Create(ImageProc.ImgToBmpSource(img)));
                using (var stream = new FileStream(savePathTxt.Text, FileMode.Create, FileAccess.Write))
                {
                    JPEGenc.Save(stream);
                }
                break;

            case ".jpg":
                var JPGenc = new JpegBitmapEncoder();
                JPGenc.Frames.Add(BitmapFrame.Create(ImageProc.ImgToBmpSource(img)));
                using (var stream = new FileStream(savePathTxt.Text, FileMode.Create, FileAccess.Write))
                {
                    JPGenc.Save(stream);
                }
                break;

            case ".png":
                var PNGenc = new PngBitmapEncoder();
                PNGenc.Frames.Add(BitmapFrame.Create(ImageProc.ImgToBmpSource(img)));
                using (var stream = new FileStream(savePathTxt.Text, FileMode.Create, FileAccess.Write))
                {
                    PNGenc.Save(stream);
                }
                break;

            case ".tif":
                var TIFenc = new TiffBitmapEncoder();
                // get TIFF compression type
                if (tifTypeCBX.SelectedIndex == 0)
                {
                    TIFenc.Compression = TiffCompressOption.Ccitt3;
                }
                if (tifTypeCBX.SelectedIndex == 1)
                {
                    TIFenc.Compression = TiffCompressOption.Ccitt4;
                }
                if (tifTypeCBX.SelectedIndex == 2)
                {
                    TIFenc.Compression = TiffCompressOption.Lzw;
                }
                if (tifTypeCBX.SelectedIndex == 3)
                {
                    TIFenc.Compression = TiffCompressOption.None;
                }
                if (tifTypeCBX.SelectedIndex == 4)
                {
                    TIFenc.Compression = TiffCompressOption.Rle;
                }
                if (tifTypeCBX.SelectedIndex == 5)
                {
                    TIFenc.Compression = TiffCompressOption.Zip;
                }
                TIFenc.Frames.Add(BitmapFrame.Create(ImageProc.ImgToBmpSource(img)));
                using (var stream = new FileStream(savePathTxt.Text, FileMode.Create, FileAccess.Write))
                {
                    TIFenc.Save(stream);
                }
                break;

            case ".tiff":
                var TIFFenc = new TiffBitmapEncoder();
                // get TIFF compression type
                if (tifTypeCBX.SelectedIndex == 0)
                {
                    TIFFenc.Compression = TiffCompressOption.Ccitt3;
                }
                if (tifTypeCBX.SelectedIndex == 1)
                {
                    TIFFenc.Compression = TiffCompressOption.Ccitt4;
                }
                if (tifTypeCBX.SelectedIndex == 2)
                {
                    TIFFenc.Compression = TiffCompressOption.Lzw;
                }
                if (tifTypeCBX.SelectedIndex == 3)
                {
                    TIFFenc.Compression = TiffCompressOption.None;
                }
                if (tifTypeCBX.SelectedIndex == 4)
                {
                    TIFFenc.Compression = TiffCompressOption.Rle;
                }
                if (tifTypeCBX.SelectedIndex == 5)
                {
                    TIFFenc.Compression = TiffCompressOption.Zip;
                }
                TIFFenc.Frames.Add(BitmapFrame.Create(ImageProc.ImgToBmpSource(img)));
                using (var stream = new FileStream(savePathTxt.Text, FileMode.Create, FileAccess.Write))
                {
                    TIFFenc.Save(stream);
                }
                break;
            }
        }
        public void InitScan()
        {
            ImageProc imgP = new ImageProc();
            CommonDialogClass commonDialogClass = new CommonDialogClass();
            DeviceIdCmb.SelectedIndex = DeviceCmb.SelectedIndex;
            try
            {
                if (DeviceCmb.Items.Count == 0)
                {
                    MessageBox.Show("Please connect a scanner.");
                }
                Deskew _deskew = new Deskew();

                scanSettings _scanSettings = new scanSettings();

                List<System.Drawing.Image> images = null;
                images = WIAScanner.AutoScan((string)DeviceIdCmb.SelectedItem, Convert.ToInt32(dpiTxt.Text), (Convert.ToDouble(cropxTxt.Text)), (Convert.ToDouble(cropyTxt.Text)),
                (Convert.ToDouble(widthTxt.Text) * Convert.ToDouble(dpiTxt.Text)), (Convert.ToDouble(heightTxt.Text) * Convert.ToDouble(dpiTxt.Text)), (int)parseSettings("Brightness"),
                (int)parseSettings("Contrast"), (int)parseSettings("Color Mode"));
                selRect.Visibility = Visibility.Collapsed;
                foreach (System.Drawing.Image image in images)
                {
                    if ((int)parseSettings("Bpp") == 1)
                    {
                        ScanView.Source = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.BlackWhite);
                        saveImageAsFile(ImageProc.setPixelFormat2(ImageProc.ImgToBmpSource(image), System.Drawing.Imaging.PixelFormat.Format1bppIndexed));
                    }
                    if ((int)parseSettings("Bpp") == 8)
                    {
                        BitmapSource img8bit = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.Gray8);
                        ScanView.Source = img8bit;
                        saveImageAsFile(ImageProc.BmpSource2Img(img8bit));
                    }
                    if ((int)parseSettings("Bpp") == 24)
                    {
                        ScanView.Source = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.Bgr24);
                        saveImageAsFile(ImageProc.setPixelFormat2(ImageProc.ImgToBmpSource(image), System.Drawing.Imaging.PixelFormat.Format24bppRgb));
                    }
                    if ((int)parseSettings("Bpp") == 32)
                    {
                        ScanView.Source = ImageProc.setPixelFormat1(ImageProc.ImgToBmpSource(image), PixelFormats.Bgr32);
                        saveImageAsFile(ImageProc.setPixelFormat2(ImageProc.ImgToBmpSource(image), System.Drawing.Imaging.PixelFormat.Format32bppRgb));
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Unexpected Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }