📄 photosadder.cs
字号:
using System;
using System.Windows.Forms;
using System.IO;
using VirtualPhotoOrganizer.Photo;
using TXML;
namespace VirtualPhotoOrganizer.Util
{
/// <summary>
/// used for adding photos to an album
/// </summary>
internal class PhotosAdder
{
// lanugae strings
private string LsAllFormats = "All supported Formats";
private string LsFiles;
private string LsFolder;
private string LsDelete;
private string LsDeleteQ;
private const string MN = "PhotosAdder";
public PhotosAdder() {
// LoadLanguageStrings();
}
/// <summary>
/// Shows an open file dialog and adds the selected photos to the specified album
/// </summary>
/// <returns>true if the dialog result was ok and false if it was cancel</returns>
public bool AddPhotosFromFiles(Album a) {
// create a new openFileDialog with the required options
System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = LsAllFormats + "|*.jpg;*.jpeg;*.tif;*.tiff;*.png;*.bmp;*.gif" + "|JPEGs (*.jpg, *.jpeg)|*.jpg;*.jpeg" + "|Tagged Image File Format (*.tif, *.tiff)|*.tif;*.tiff" + "|Portable Network Graphics (*.png)|*.png" + "|Windows Bitmaps (*.bmp)|*.bmp" + "|CompuServe Graphics Interchange (*.gif)|*.gif";
ofd.FilterIndex = 0;
// open the dialog, create a photos list and add the photos to the album
if (ofd.ShowDialog() == DialogResult.OK) {
bool deleteOrigs = false;
if (MessageBox.Show(LsDeleteQ, LsDelete, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
deleteOrigs = true;
AlbumCreator ac = new AlbumCreator();
ac.AddPhotosToAlbum(a, GetPhotos(ofd.FileNames), deleteOrigs);
return true;
} else
return false;
}
/// <summary>
/// Shows a FolderBrowserDialog and adds all the supported photos in it to the specified album
/// </summary>
/// <returns>true if the dialog result was ok and false if it was cancel</returns>
public bool AddPhotosFromDirectory(Album a) {
// create a new folderBrowserDialog and show it
System.Windows.Forms.FolderBrowserDialog fbd = new FolderBrowserDialog();
// open the dialog, create a photos list and add the photos to the album
if (fbd.ShowDialog() == DialogResult.OK) {
bool deleteOrigs = false;
if (MessageBox.Show(LsDeleteQ, LsDelete, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
deleteOrigs = true;
AlbumCreator ac = new AlbumCreator();
ac.AddPhotosToAlbum(a, GetPhotos(new DirectoryInfo(fbd.SelectedPath)), deleteOrigs);
return true;
} else
return false;
}
#region helper methods
private void LoadLanguageStrings() {
try {
TXmlReader reader = XmlHandler.OpenLangFile();
LsAllFormats = reader.GetString(MN, "AllFormats", "All supported Formats");
LsFiles = reader.GetString(MN, "Files", "Add Photos from Files");
LsFolder = reader.GetString(MN, "Folder", "Add Photos from Folder");
LsDeleteQ = reader.GetString(MN, "DeleteQ", "Do you want to delete the original images after copying them?");
LsDelete = reader.GetString(MN, "Delete", "Delte Originals?");
reader.Close();
}
catch {
LsAllFormats = "All supported Formats";
LsFiles = "Add Photos from Files";
LsFolder = "Add Photos from Folder";
LsDeleteQ = "Do you want to delete the original images after copying them?";
LsDelete = "Delte Originals?";
}
}
/// <summary>
/// returns a list of photos from a string array
/// </summary>
private Photos GetPhotos(string[] fileNames) {
Photos p = new Photos();
foreach (string s in fileNames)
p.Add(new Photo.Photo(s, "", GetFileNameFromPath(s), "", ""));
return p;
}
/// <summary>
/// returns a list of photos from a directory
/// </summary>
private Photos GetPhotos(DirectoryInfo dir) {
Photos p = new Photos();
string name;
foreach (FileInfo fi in dir.GetFiles()) {
name = fi.FullName;
// add all files with a supported extension
switch (fi.Extension.ToLower()) {
case ".jpg": p.Add(new Photo.Photo(name, "", GetFileNameFromPath(name), "", ""));
break;
case ".tif": p.Add(new Photo.Photo(name, "", GetFileNameFromPath(name), "", ""));
break;
case ".png": p.Add(new Photo.Photo(name, "", GetFileNameFromPath(name), "", ""));
break;
case ".bmp": p.Add(new Photo.Photo(name, "", GetFileNameFromPath(name), "", ""));
break;
case ".gif": p.Add(new Photo.Photo(name, "", GetFileNameFromPath(name), "", ""));
break;
case ".jpeg": p.Add(new Photo.Photo(name, "", GetFileNameFromPath(name), "", ""));
break;
case ".tiff": p.Add(new Photo.Photo(name, "", GetFileNameFromPath(name), "", ""));
break;
default:
break;
}
}
return p;
}
/// <summary>
/// Removes the directories from a given path and returnes only the file name
/// </summary>
private string GetFileNameFromPath(string path) {
FileInfo fi = new FileInfo(path);
return fi.Name;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -