Exemplo n.º 1
0
        private void GetImageFileFromSystem()
        {
            try
            {
                SetMode(Modes.SingleImage);
                // The primary thing that OpenFileDialogWithViews buys us is the ability to default to large icons.
                // OpenFileDialogWithViews still doesn't let us read (and thus remember) the selected view.
                using (var dlg = new OpenFileDialogWithViews(OpenFileDialogWithViews.DialogViewTypes.Large_Icons))
                {
                    if (string.IsNullOrEmpty(ImageToolboxSettings.Default.LastImageFolder))
                    {
                        dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                    }
                    else
                    {
                        dlg.InitialDirectory = ImageToolboxSettings.Default.LastImageFolder;;
                    }

                    dlg.Title = "Choose Picture File".Localize("ImageToolbox.FileChooserTitle", "Title shown for a file chooser dialog brought up by the ImageToolbox");

                    //NB: dissallowed gif because of a .net crash:  http://jira.palaso.org/issues/browse/BL-85
                    dlg.Filter = "picture files".Localize("ImageToolbox.PictureFiles", "Shown in the file-picking dialog to describe what kind of files the dialog is filtering for") + "(*.png;*.tif;*.tiff;*.jpg;*.jpeg;*.bmp)|*.png;*.tif;*.tiff;*.jpg;*.jpeg;*.bmp;";

                    if (DialogResult.OK == dlg.ShowDialog(this.ParentForm))
                    {
                        ImageToolboxSettings.Default.LastImageFolder = Path.GetDirectoryName(dlg.FileName);
                        ImageToolboxSettings.Default.Save();

                        try
                        {
                            _currentImage = PalasoImage.FromFile(dlg.FileName);
                        }
                        catch (Exception err)                         //for example, http://jira.palaso.org/issues/browse/BL-199
                        {
                            var msg = "Sorry, there was a problem loading that image.".Localize(
                                "ImageToolbox.ProblemLoadingImage");
                            if (ImageLoadingExceptionReporter != null)
                            {
                                ImageLoadingExceptionReporter(dlg.FileName, err, msg);
                            }
                            else
                            {
                                ErrorReport.NotifyUserOfProblem(err, msg);
                            }

                            return;
                        }
                        _pictureBox.Image = _currentImage.Image;
                        if (ImageChanged != null)
                        {
                            ImageChanged.Invoke(this, null);
                        }
                    }
                }
            }
            finally
            {
                _actModal = false;
            }
        }
 private void OpenFileFromDrag(string path)
 {
     SetMode(Modes.SingleImage);
     _currentImage     = PalasoImage.FromFile(path);
     _pictureBox.Image = _currentImage.Image;
     ImageToolboxSettings.Default.LastImageFolder = Path.GetDirectoryName(path);
     ImageToolboxSettings.Default.Save();
     if (ImageChanged != null)
     {
         ImageChanged.Invoke(this, null);
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Load a PalasoImage from a file, trying several times if needed.
 /// </summary>
 /// <remarks>
 /// This would logically belong in SIL.Core.IO.RobustIO except that PalasoImage is in SIL.Windows.Forms.
 /// </remarks>
 public static PalasoImage FromFileRobustly(string path)
 {
     return(RetryUtility.Retry(() => PalasoImage.FromFile(path),
                               RetryUtility.kDefaultMaxRetryAttempts,
                               RetryUtility.kDefaultRetryDelay,
                               new HashSet <Type>
     {
         Type.GetType("System.IO.IOException"),
         // Odd type to catch... but it seems that Image.FromFile (which is called in the bowels of PalasoImage.FromFile)
         // throws OutOfMemoryException when the file is inaccessible.
         // See http://stackoverflow.com/questions/2610416/is-there-a-reason-image-fromfile-throws-an-outofmemoryexception-for-an-invalid-i
         Type.GetType("System.OutOfMemoryException")
     }));
 }
        private void GetFromDevice(ImageAcquisitionService.DeviceKind deviceKind)
        {
            //_pictureBox.Image = SampleImages.sampleScan;
            try
            {
                var acquisitionService = new ImageAcquisitionService(deviceKind);

                var wiaImageFile = acquisitionService.Acquire();
                if (wiaImageFile == null)
                {
                    return;
                }

                var imageFile = ConvertToPngOrJpegIfNotAlready(wiaImageFile);
                _currentImage     = PalasoImage.FromFile(imageFile);
                _pictureBox.Image = _currentImage.Image;

                if (ImageChanged != null)
                {
                    ImageChanged.Invoke(this, null);
                }
            }
            catch (ImageDeviceNotFoundException error)
            {
                _messageLabel.Text = error.Message + Environment.NewLine + Environment.NewLine +
                                     "Note: this program works with devices that have a 'WIA' driver, not the old-style 'TWAIN' driver";
                _messageLabel.Visible = true;
            }
            catch (WIA_Version2_MissingException)
            {
                _messageLabel.Text    = "Windows XP does not come with a crucial DLL that lets you use a WIA scanner with this program. Get a technical person to downloand and follow the directions at http://vbnet.mvps.org/files/updates/wiaautsdk.zip";
                _messageLabel.Visible = true;
            }
            catch (Exception error)
            {
                ErrorReport.NotifyUserOfProblem(error, "Problem Getting Image".Localize("ImageToolbox.ProblemGettingImageFromDevice"));
            }
        }