예제 #1
0
        private void OnSearchGalleryLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            var         searchString = SearchTermProvider == null ? string.Empty : SearchTermProvider.SearchString;
            PalasoImage currentImage = null;

            try
            {
                if (!string.IsNullOrEmpty(_relativePathToImage) && File.Exists(GetPathToImage()))
                {
                    currentImage = PalasoImage.FromFile(GetPathToImage());
                }
            }
            catch (Exception)
            {
                //if we couldn't load it (like if it's missing), best to carry on and let them pick a new one
            }

            using (var dlg = new Palaso.UI.WindowsForms.ImageToolbox.ImageToolboxDialog(currentImage ?? new PalasoImage(), searchString))
            {
                if (DialogResult.OK == dlg.ShowDialog(this.ParentForm))
                {
                    try
                    {
                        if (File.Exists(GetPathToImage()))
                        {
                            File.Delete(GetPathToImage());
                        }
                        string fileName = CheckFileName(searchString);
                        if (string.IsNullOrEmpty(fileName))
                        {
                            fileName = DateTime.UtcNow.ToFileTimeUtc().ToString();
                        }

                        string fileExt;
                        if ((!String.IsNullOrEmpty(dlg.ImageInfo.FileName)) && (!String.IsNullOrEmpty(dlg.ImageInfo.FileName.Split('.').Last())))
                        {
                            fileExt = "." + dlg.ImageInfo.FileName.Split('.').Last();
                        }
                        else
                        {
                            // If no file name or extension, default to png
                            fileExt = ".png";
                        }
                        fileExt = PalasoImage.FileExtForWebFormat(fileExt);

                        //NB: we have very possible collision if use a real word "bird".
                        //Less so with a time "3409343839", which this only uses if we don't have a file name (e.g. if it came from a scanner)
                        //so this will add to the name if what we have is not unique.
                        if (File.Exists(Path.Combine(_storageFolderPath, fileName + fileExt)))
                        {
                            fileName += "-" + DateTime.UtcNow.ToFileTimeUtc();
                        }

                        fileName += fileExt;
                        var fullDestPath = Path.Combine(_storageFolderPath, fileName);
                        _relativePathToImage = fullDestPath.Replace(_pathToReferingFile, "");
                        _relativePathToImage = _relativePathToImage.Trim(Path.DirectorySeparatorChar);

                        dlg.ImageInfo.Save(GetPathToImage());
                        UpdateDisplay();
                        NotifyChanged();
                    }
                    catch (Exception error)
                    {
                        ErrorReport.NotifyUserOfProblem("WeSay was not able to save the picture file.\r\n{0}", error.Message);
                    }
                }
            }
        }