示例#1
0
        public static DatabaseFin FullyLoadFin(DatabaseFin fin)
        {
            DatabaseFin finCopy = new DatabaseFin(fin);

            // TODO: Cache images?
            if (!string.IsNullOrEmpty(finCopy.ImageFilename))
            {
                CatalogSupport.UpdateFinFieldsFromImage(Options.CurrentUserOptions.CurrentSurveyAreaPath, finCopy);

                string fullImageFilename = Path.Combine(Options.CurrentUserOptions.CurrentSurveyAreaPath,
                                                        (string.IsNullOrEmpty(finCopy.OriginalImageFilename)) ? finCopy.ImageFilename : finCopy.OriginalImageFilename);

                if (File.Exists(fullImageFilename))
                {
                    var img = System.Drawing.Image.FromFile(fullImageFilename);

                    var bitmap = new Bitmap(img);
                    // TODO: Hack for HiDPI -- this should be more intelligent.
                    bitmap.SetResolution(96, 96);

                    finCopy.OriginalFinImage = new Bitmap(bitmap);

                    // TODO: Refactor this so we're not doing it every time, which is a little crazy
                    if (finCopy.ImageMods != null && finCopy.ImageMods.Count > 0)
                    {
                        bitmap = ModificationHelper.ApplyImageModificationsToOriginal(bitmap, finCopy.ImageMods);
                        // TODO: HiDPI hack
                        bitmap.SetResolution(96, 96);
                    }

                    finCopy.FinImage = bitmap;
                }
            }

            if (!string.IsNullOrEmpty(finCopy.OriginalImageFilename) && !File.Exists(finCopy.OriginalImageFilename))
            {
                finCopy.OriginalImageFilename = Path.Combine(Options.CurrentUserOptions.CurrentSurveyAreaPath, finCopy.OriginalImageFilename);
            }

            return(finCopy);
        }
示例#2
0
        public static DatabaseFin OpenFinz(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }

            string uniqueDirectoryName = Path.GetFileName(filename).Replace(".", string.Empty) + "_" + Guid.NewGuid().ToString().Replace("-", string.Empty);
            string fullDirectoryName   = Path.Combine(Path.GetTempPath(), uniqueDirectoryName);

            try
            {
                Directory.CreateDirectory(fullDirectoryName);

                ZipFile.ExtractToDirectory(filename, fullDirectoryName);

                string dbFilename = Path.Combine(fullDirectoryName, FinzDatabaseFilename);

                if (!File.Exists(dbFilename))
                {
                    return(null);
                }

                var db = OpenDatabase(dbFilename, Options.CurrentUserOptions.DefaultCatalogScheme, false);

                // First and only fin
                var fin = db.AllFins[0];

                fin.FinFilename = filename;

                var baseimgfilename = Path.GetFileName(fin.ImageFilename);
                fin.ImageFilename = Path.Combine(fullDirectoryName, baseimgfilename);

                List <ImageMod> imageMods;
                bool            thumbOnly;
                string          originalFilenameFromPng;
                float           normScale;

                // The old version kept this info inside PNG text.  The newer version saves it all in the SQLite database.
                PngHelper.ParsePngText(fin.ImageFilename, out normScale, out imageMods, out thumbOnly, out originalFilenameFromPng);

                if (imageMods != null && imageMods.Count > 0)
                {
                    fin.ImageMods = imageMods;
                }

                if (normScale != 1.0)
                {
                    fin.Scale = normScale;
                }

                string originalFilenameToUse = (string.IsNullOrEmpty(originalFilenameFromPng)) ? fin.OriginalImageFilename : originalFilenameFromPng;

                if (!string.IsNullOrEmpty(originalFilenameToUse))
                {
                    fin.OriginalImageFilename = Path.Combine(fullDirectoryName, Path.GetFileName(originalFilenameToUse));

                    // We're loading the image this way because Bitmap keeps a lock on the original file, and
                    // we want to try to delete the file below.  So we open the file in another object in a using statement
                    // then copy it over to our actual working object.
                    using (var originalImageFromFile = (Bitmap)Image.FromFile(fin.OriginalImageFilename))
                    {
                        fin.OriginalFinImage = new Bitmap(originalImageFromFile);
                        fin.OriginalFinImage?.SetResolution(96, 96);

                        if (fin.ImageMods != null)
                        {
                            fin.FinImage = ModificationHelper.ApplyImageModificationsToOriginal(fin.OriginalFinImage, fin.ImageMods);
                        }
                        else
                        {
                            fin.FinImage = new Bitmap(fin.OriginalFinImage);
                        }

                        fin.FinImage?.SetResolution(96, 96);
                    }
                }

                // TODO: Do something with thumbOnly?

                // We're loading the image this way because Bitmap keeps a lock on the original file, and
                // we want to try to delete the file below.  So we open the file in another object in a using statement
                // then copy it over to our actual working object.
                //using (var imageFromFile = (Bitmap)Image.FromFile(fin.ImageFilename))
                //{
                //	fin.FinImage = new Bitmap(imageFromFile);
                //	fin.FinImage?.SetResolution(96, 96);
                //}

                return(fin);
            }
            catch
            {
                // TODO: Probably should have better handling here
                return(null);
            }
            finally
            {
                try
                {
                    Trace.WriteLine("Trying to remove temporary files for finz file.");

                    SQLiteConnection.ClearAllPools();

                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    if (Directory.Exists(fullDirectoryName))
                    {
                        Directory.Delete(fullDirectoryName, true);
                    }
                }
                catch (Exception ex)
                {
                    Trace.Write("Couldn't remove temporary files:");
                    Trace.WriteLine(ex);
                }
            }
        }