public bool CheckImage(FilePath path) { int width, height; string errorTitle = null; string errorMessage = null; using (var type = Pixbuf.GetFileInfo(path, out width, out height)) { if (type == null) { errorTitle = GettextCatalog.GetString("Invalid file selected"); errorMessage = GettextCatalog.GetString("Selected file was not a valid image."); } else if (type.IsDisabled) { errorTitle = GettextCatalog.GetString("Unsupported image selected"); errorMessage = GettextCatalog.GetString("Support for loading images of type '{0}' has not been enabled.", type.Name); } else if (AcceptedSize != Size.Empty && AcceptedSize != new Size(width, height)) { errorTitle = GettextCatalog.GetString("Incorrect image dimensions"); errorMessage = GettextCatalog.GetString( "Only images with size {0}x{1} are allowed. Picture was {2}x{3}.", AcceptedSize.Width, AcceptedSize.Height, width, height); } else if (!SupportedFormats.Contains(type.Name)) { var formats = string.Join(", ", SupportedFormats.Select(f => "'" + f + "'")); errorTitle = GettextCatalog.GetString("Invalid image selected"); errorMessage = GettextCatalog.GetString("An image of type '{0}' has been selected but you must select an image of type '{1}'.", type.Name, formats); } else { return(true); } LoggingService.LogError("{0}: {1}", errorTitle, errorMessage); MessageService.ShowError(errorTitle, errorMessage); return(false); } }
protected override void OnClicked() { base.OnClicked(); if (project == null) { return; } var formats = string.Join("|", SupportedFormats.Select(f => "*." + f)); var dialog = new ProjectFileSelectorDialog(project, "All Images", formats); try { if (AcceptedSize.IsEmpty) { dialog.Title = GettextCatalog.GetString("Select image..."); } else { dialog.Title = GettextCatalog.GetString("Select image ({0}x{1})...", RecommendedSize.Width, RecommendedSize.Height); } while (MessageService.RunCustomDialog(dialog) == (int)Gtk.ResponseType.Ok && dialog.SelectedFile != null) { var path = dialog.SelectedFile.FilePath; if (!CheckImage(path)) { continue; } SelectedProjectFile = dialog.SelectedFile.ProjectVirtualPath; OnChanged(EventArgs.Empty); break; } } finally { dialog.Destroy(); } }