Пример #1
0
        private void AutoCrop(AttachmentDialogImageItem img)
        {
            CannyEdgeDetector edgeDetector = new CannyEdgeDetector(img.Bitmap, 20, 80, 30);

            edgeDetector.DetectEdges();
            Rect bounds = edgeDetector.EdgeBounds;

            AddResizer(img, bounds);
            SetDirty();
        }
Пример #2
0
        private void OnCropImage(object sender, ExecutedRoutedEventArgs e)
        {
            AttachmentDialogImageItem img = this.selected as AttachmentDialogImageItem;

            if (img == null)
            {
                return;
            }
            AutoCrop(img);
        }
Пример #3
0
        private void RotateLeft(object sender, ExecutedRoutedEventArgs e)
        {
            AttachmentDialogImageItem img = this.selected as AttachmentDialogImageItem;

            if (img == null)
            {
                return;
            }
            img.RotateImage(-90.0);
            OnItemChanged(img);
        }
Пример #4
0
        private void Scan(object sender, RoutedEventArgs e)
        {
            try
            {
                AppBarButton button = sender as AppBarButton;
                // hmmm, reusing the device doesn't work because it somehow resets itself to capture higher resolution images.
                bool hasDevice = false; //  device != null;

                GetScannerAsync(new Action(() =>
                {
                    WIA.ImageFile imageFile = null;

                    if (!hasDevice)
                    {
                        imageFile = globalDialog.ShowAcquireImage(DeviceType: WIA.WiaDeviceType.ScannerDeviceType,
                                                                  Bias: WIA.WiaImageBias.MinimizeSize, Intent: WIA.WiaImageIntent.TextIntent, AlwaysSelectDevice: false);
                    }
                    else
                    {
                        WIA.Item scannerItem      = device.Items[1];
                        const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
                        object scanResult         = globalDialog.ShowTransfer(scannerItem, wiaFormatPNG, false);
                        imageFile = (WIA.ImageFile)scanResult;
                    }

                    if (imageFile != null)
                    {
                        string temp = System.IO.Path.GetTempFileName();
                        if (File.Exists(temp))
                        {
                            File.Delete(temp);
                        }
                        imageFile.SaveFile(temp);
                        TempFilesManager.AddTempFile(temp);

                        AttachmentDialogImageItem image = new AttachmentDialogImageItem(temp);
                        AddItem(image);
                        LayoutContent();
                        SelectItem(image);
                        AutoCrop(image);
                    }
                }));
            }
            catch (Exception ex)
            {
                device = null;
                string message = GetWiaErrorMessage(ex);
                MessageBox.Show(this, message, "Scan Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Пример #5
0
        private void Save(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                int index = 0;
                foreach (UIElement element in Canvas.Children)
                {
                    AttachmentDialogItem item = element as AttachmentDialogItem;
                    if (item != null)
                    {
                        string fileName = Manager.GetUniqueFileName(this.Transaction, item.FileExtension);
                        string existing = item.FileName;
                        if (!string.IsNullOrEmpty(existing) && string.Compare(fileName, existing, StringComparison.OrdinalIgnoreCase) != 0)
                        {
                            // file name is different, so delete the old file.
                            TempFilesManager.DeleteFile(existing);
                        }

                        AttachmentDialogImageItem img = item as AttachmentDialogImageItem;
                        if (img != null)
                        {
                            if (item == selected && resizer != null)
                            {
                                // crop it.
                                Rect bounds = GetUnscaledBounds(resizer.Bounds);
                                img.Resize(bounds);
                            }
                        }

                        // in case an item was deleted in the middle, we need to re-index the items.
                        item.Save(fileName);
                        item.FileName = fileName;
                        index++;
                    }
                }

                ClearSelection();
                LoadAttachments();
                dirty = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Unexpected error saving new image: " + ex.Message, "Save Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Пример #6
0
        private void Paste(object sender, ExecutedRoutedEventArgs e)
        {
            AttachmentDialogItem newItem = null;

            if (Clipboard.ContainsImage())
            {
                var image = Clipboard.GetImage();
                if (image != null)
                {
                    // for some reason this bitmap doesn't paint unless I save and reload it
                    // the in-memory copy from the clipboard is a bit touchy, probably comes from
                    // another process and so on, so persistence is better strategy here...
                    string path = Path.GetTempFileName();

                    AttachmentDialogImageItem item = new AttachmentDialogImageItem(image);
                    item.Save(path);

                    TempFilesManager.AddTempFile(path);

                    newItem = new AttachmentDialogImageItem(path);
                }
            }
            else if (Clipboard.ContainsData(DataFormats.XamlPackage) ||
                     Clipboard.ContainsData(DataFormats.Rtf) ||
                     Clipboard.ContainsData(DataFormats.Text))
            {
                newItem = new AttachmentDialogDocumentItem(Clipboard.GetDataObject());
            }

            if (newItem != null)
            {
                AddItem(newItem);
                LayoutContent();
                SelectItem(newItem);
                SetDirty();
            }
        }