Extracted some code from the Desktop form to a new ImageSaver type for reuseability in the console project.

This commit is contained in:
Ben Olden-Cooligan 2013-07-07 13:16:18 -04:00
parent 477f5bbbde
commit b4f8dd3505
6 changed files with 132 additions and 76 deletions

View File

@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters /&gt;&lt;/data&gt;</s:String>
<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue">&lt;data /&gt;</s:String></wpf:ResourceDictionary>

View File

@ -34,14 +34,14 @@ namespace NAPS2
public partial class FDesktop : Form, IScanReceiver
{
private readonly IEmailer emailer;
private readonly ImageFileNamer imageFileNamer;
private readonly ImageSaver imageSaver;
private readonly ScannedImageList imageList = new ScannedImageList();
public FDesktop(IEmailer emailer, ImageFileNamer imageFileNamer)
public FDesktop(IEmailer emailer, ImageSaver imageSaver)
{
InitializeComponent();
this.emailer = emailer;
this.imageFileNamer = imageFileNamer;
this.imageSaver = imageSaver;
}
private IEnumerable<int> SelectedIndices
@ -221,77 +221,16 @@ namespace NAPS2
if (sd.ShowDialog() == DialogResult.OK)
{
ImageFormat format = GetImageFormat(sd.FileName);
int i = 0;
if (imageList.Images.Count == 1)
{
using (Bitmap baseImage = imageList.Images[0].GetImage())
{
baseImage.Save(sd.FileName, format);
}
return;
}
if (format == ImageFormat.Tiff)
{
Image[] bitmaps = imageList.Images.Select(x => x.GetImage()).ToArray();
TiffHelper.SaveMultipage(bitmaps, sd.FileName);
foreach (Image bitmap in bitmaps)
{
bitmap.Dispose();
}
return;
}
var fileNames = imageFileNamer.GetFileNames(sd.FileName, imageList.Images.Count).GetEnumerator();
foreach (ScannedImage img in imageList.Images)
{
using (Bitmap baseImage = img.GetImage())
{
fileNames.MoveNext();
baseImage.Save(fileNames.Current, format);
}
i++;
}
imageSaver.SaveImages(sd.FileName, imageList.Images);
}
}
}
private ImageFormat GetImageFormat(string fileName)
{
string extension = Path.GetExtension(fileName);
switch (extension.ToLower())
{
case ".bmp":
return ImageFormat.Bmp;
case ".emf":
return ImageFormat.Emf;
case ".gif":
return ImageFormat.Gif;
case ".ico":
return ImageFormat.Icon;
case ".jpg":
case ".jpeg":
return ImageFormat.Jpeg;
case ".png":
return ImageFormat.Png;
case ".tif":
case ".tiff":
return ImageFormat.Tiff;
case ".wmf":
return ImageFormat.Wmf;
default:
return ImageFormat.Jpeg;
}
}
private void tsPDFEmail_Click(object sender, EventArgs e)
{
if (imageList.Images.Count > 0)
{
string path = Application.StartupPath + "\\Scan.pdf";
string path = Paths.AppData + "\\Scan.pdf";
exportPDF(path);
emailer.SendEmail(path, "");
File.Delete(path);

89
NAPS2/ImageSaver.cs Normal file
View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using NAPS2.Scan;
namespace NAPS2
{
public class ImageSaver
{
private readonly ImageFileNamer imageFileNamer;
public ImageSaver(ImageFileNamer imageFileNamer)
{
this.imageFileNamer = imageFileNamer;
}
public void SaveImages(string fileName, ICollection<IScannedImage> images)
{
ImageFormat format = GetImageFormat(fileName);
int i = 0;
if (images.Count == 1)
{
using (Bitmap baseImage = images.First().GetImage())
{
baseImage.Save(fileName, format);
}
return;
}
if (format == ImageFormat.Tiff)
{
Image[] bitmaps = images.Select(x => x.GetImage()).ToArray();
TiffHelper.SaveMultipage(bitmaps, fileName);
foreach (Image bitmap in bitmaps)
{
bitmap.Dispose();
}
return;
}
var fileNames = imageFileNamer.GetFileNames(fileName, images.Count).GetEnumerator();
foreach (ScannedImage img in images)
{
using (Bitmap baseImage = img.GetImage())
{
fileNames.MoveNext();
baseImage.Save(fileNames.Current, format);
}
i++;
}
}
private static ImageFormat GetImageFormat(string fileName)
{
string extension = Path.GetExtension(fileName);
Debug.Assert(extension != null);
switch (extension.ToLower())
{
case ".bmp":
return ImageFormat.Bmp;
case ".emf":
return ImageFormat.Emf;
case ".gif":
return ImageFormat.Gif;
case ".ico":
return ImageFormat.Icon;
case ".jpg":
case ".jpeg":
return ImageFormat.Jpeg;
case ".png":
return ImageFormat.Png;
case ".tif":
case ".tiff":
return ImageFormat.Tiff;
case ".wmf":
return ImageFormat.Wmf;
default:
return ImageFormat.Jpeg;
}
}
}
}

View File

@ -99,6 +99,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CollectionExtensions.cs" />
<Compile Include="ImageSaver.cs" />
<Compile Include="ImageFileNamer.cs" />
<Compile Include="ImageHelper.cs" />
<Compile Include="Icons.Designer.cs">
@ -109,6 +110,7 @@
<Compile Include="IProfileManager.cs" />
<Compile Include="IScanPerformer.cs" />
<Compile Include="IScanReceiver.cs" />
<Compile Include="Paths.cs" />
<Compile Include="ScannedImageList.cs" />
<Compile Include="ScanPerformer.cs" />
<Compile Include="Scan\Stub\StubScanDriver.cs" />

33
NAPS2/Paths.cs Normal file
View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
#if !STANDALONE
using System.IO;
#endif
using System.Linq;
using System.Text;
#if STANDALONE
using System.Windows.Forms;
#endif
namespace NAPS2
{
public static class Paths
{
#if STANDALONE
private static readonly string AppDataPath = Application.StartupPath;
#else
private static readonly string AppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NAPS2");
#endif
public static string AppData
{
get
{
if (!Directory.Exists(AppDataPath))
{
Directory.CreateDirectory(AppDataPath);
}
return AppDataPath;
}
}
}
}

View File

@ -30,12 +30,8 @@ namespace NAPS2
public class ProfileManager : IProfileManager
{
private const string ProfilesFileName = "profiles.xml";
#if STANDALONE
private static readonly string ProfilesFolder = Application.StartupPath;
#else
private static readonly string ProfilesFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NAPS2");
#endif
private static readonly string ProfilesPath = Path.Combine(ProfilesFolder, ProfilesFileName);
private static readonly string ProfilesPath = Path.Combine(Paths.AppData, ProfilesFileName);
private static readonly string OldProfilesPath = Path.Combine(Application.StartupPath, "profiles.xml");
@ -89,10 +85,6 @@ namespace NAPS2
public void Save()
{
if (!Directory.Exists(ProfilesFolder))
{
Directory.CreateDirectory(ProfilesFolder);
}
using (Stream strFile = File.Open(ProfilesPath, FileMode.Create))
{
var serializer = new XmlSerializer(typeof(List<ScanSettings>));