public void GetMetadata(FileItem fileItem)
        {
            try
            {
                PhotoUtils.WaitForFile(fileItem.FileName);
                Exiv2Helper exiv2Helper = new Exiv2Helper();
                exiv2Helper.Load(fileItem);

                if (fileItem.FileInfo.ExifTags.ContainName("Exif.Image.Orientation"))
                {
                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "bottom, right")
                    {
                        fileItem.AutoRotation = 2;
                    }

                    //if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "top, left")
                    //    writeableBitmap = writeableBitmap.Rotate(180);

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "right, top")
                    {
                        fileItem.AutoRotation = 1;
                    }

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "left, bottom")
                    {
                        fileItem.AutoRotation = 3;
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error("Error loading metadata ", exception);
            }
        }
 /// <summary>
 /// Will load the small thumbnail attached to file item
 /// If the cach not yet generated will retun null
 /// </summary>
 /// <param name="fileItem"></param>
 /// <param name="width">The desired with, if 0 the full size will be loaded</param>
 /// <returns></returns>
 public WriteableBitmap LoadSmallImage(FileItem fileItem, int width = 0)
 {
     if (!File.Exists(fileItem.SmallThumb))
     {
         return(null);
     }
     PhotoUtils.WaitForFile(fileItem.SmallThumb);
     try
     {
         BitmapImage bi = new BitmapImage();
         bi.BeginInit();
         if (width > 0)
         {
             bi.DecodePixelWidth = width;
         }
         bi.CacheOption = BitmapCacheOption.OnLoad;
         bi.UriSource   = new Uri(fileItem.SmallThumb);
         bi.EndInit();
         WriteableBitmap bitmap = BitmapFactory.ConvertToPbgra32Format(bi);
         //bitmap.Freeze();
         return(bitmap);
     }
     catch (Exception exception)
     {
         Log.Error("Error loading image", exception);
     }
     return(null);
 }
Beispiel #3
0
 public void RemoveThumbs()
 {
     try
     {
         if (File.Exists(SmallThumb))
         {
             PhotoUtils.WaitForFile(SmallThumb);
             File.Delete(SmallThumb);
         }
         if (File.Exists(LargeThumb))
         {
             PhotoUtils.WaitForFile(LargeThumb);
             File.Delete(LargeThumb);
         }
         if (File.Exists(InfoFile))
         {
             PhotoUtils.WaitForFile(InfoFile);
             File.Delete(InfoFile);
         }
     }
     catch (Exception ex)
     {
         Log.Error("Unable to remove thumb data", ex);
     }
 }
Beispiel #4
0
 public void LoadInfo()
 {
     try
     {
         if (File.Exists(InfoFile))
         {
             PhotoUtils.WaitForFile(InfoFile);
             XmlSerializer mySerializer =
                 new XmlSerializer(typeof(FileInfo));
             FileStream myFileStream = new FileStream(InfoFile, FileMode.Open);
             FileInfo = (FileInfo)mySerializer.Deserialize(myFileStream);
             myFileStream.Close();
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
 }
        public WriteableBitmap LoadImage(FileItem fileItem, bool fullres, bool showfocuspoints)
        {
            if (fileItem == null)
            {
                return(null);
            }
            if (!File.Exists(fileItem.LargeThumb) && !fullres)
            {
                return(null);
            }
            if (File.Exists(fileItem.InfoFile))
            {
                fileItem.LoadInfo();
            }
            if (fileItem.FileInfo == null)
            {
                fileItem.FileInfo = new FileInfo();
            }

            try
            {
                BitmapDecoder bmpDec = null;
                if (fullres && fileItem.IsRaw)
                {
                    try
                    {
                        string dcraw_exe = Path.Combine(Settings.ApplicationFolder, "dcraw.exe");
                        if (File.Exists(dcraw_exe))
                        {
                            string thumb = Path.Combine(Path.GetTempPath(),
                                                        Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                            PhotoUtils.RunAndWait(dcraw_exe,
                                                  string.Format(" -e -O \"{0}\" \"{1}\"", thumb, fileItem.FileName));
                            if (File.Exists(thumb))
                            {
                                bmpDec = BitmapDecoder.Create(new Uri(thumb), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                                File.Delete(thumb);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Log.Error("Error get dcraw thumb", exception);
                    }
                }

                PhotoUtils.WaitForFile(fullres ? fileItem.FileName : fileItem.LargeThumb);
                if (bmpDec == null)
                {
                    bmpDec = BitmapDecoder.Create(new Uri(fullres ? fileItem.FileName : fileItem.LargeThumb),
                                                  BitmapCreateOptions.None,
                                                  BitmapCacheOption.OnLoad);
                }
                // if no future processing required
                if (!showfocuspoints && ServiceProvider.Settings.RotateIndex == 0 && !ServiceProvider.Settings.FlipPreview)
                {
                    fileItem.Loading = false;
                    var b = new WriteableBitmap(bmpDec.Frames[0]);
                    b.Freeze();
                    return(b);
                }

                var bitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]);

                if (showfocuspoints && !fileItem.Transformed)
                {
                    DrawFocusPoints(fileItem, bitmap);
                }


                if (ServiceProvider.Settings.RotateIndex != 0)
                {
                    switch (ServiceProvider.Settings.RotateIndex)
                    {
                    case 1:
                        bitmap = bitmap.Rotate(90);
                        break;

                    case 2:
                        bitmap = bitmap.Rotate(180);
                        break;

                    case 3:
                        bitmap = bitmap.Rotate(270);
                        break;
                    }
                }

                if (ServiceProvider.Settings.FlipPreview)
                {
                    bitmap = bitmap.Flip(WriteableBitmapExtensions.FlipMode.Vertical);
                }

                bitmap.Freeze();
                return(bitmap);
            }
            catch (Exception exception)
            {
                Log.Error("Error loading image", exception);
                if (exception.GetType() == typeof(OutOfMemoryException) && fullres)
                {
                    return(LoadImage(fileItem, false));
                }
            }
            return(null);
        }
        public void GenerateCache(FileItem fileItem)
        {
            bool deleteFile = false;

            if (fileItem == null)
            {
                return;
            }
            if (!File.Exists(fileItem.FileName))
            {
                return;
            }

            if ((File.Exists(fileItem.LargeThumb) && File.Exists(fileItem.SmallThumb)) && File.Exists(fileItem.InfoFile))
            {
                return;
            }

            if (fileItem.Loading)
            {
                return;
            }

            fileItem.Loading = true;

            PhotoUtils.WaitForFile(fileItem.FileName);
            string filename = fileItem.FileName;

            if (fileItem.IsMovie)
            {
                try
                {
                    string ffmpeg_exe = Path.Combine(Settings.ApplicationFolder, "ffmpeg.exe");
                    if (File.Exists(ffmpeg_exe))
                    {
                        string thumb = Path.Combine(Path.GetDirectoryName(fileItem.FileName),
                                                    Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                        PhotoUtils.RunAndWait(ffmpeg_exe, String.Format("-i \"{0}\" -ss 00:00:01.000 -f image2 -vframes 1 \"{1}\"", fileItem.FileName, thumb));
                        if (File.Exists(thumb))
                        {
                            deleteFile = true;
                            filename   = thumb;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Error get video thumb", exception);
                }
            }
            if (fileItem.IsRaw)
            {
                try
                {
                    string dcraw_exe = Path.Combine(Settings.ApplicationFolder, "dcraw.exe");
                    if (File.Exists(dcraw_exe))
                    {
                        string thumb = Path.Combine(Path.GetTempPath(),
                                                    Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                        PhotoUtils.RunAndWait(dcraw_exe,
                                              string.Format(" -e -O \"{0}\" \"{1}\"", thumb, fileItem.FileName));
                        if (File.Exists(thumb))
                        {
                            deleteFile = true;
                            filename   = thumb;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Error get dcraw thumb", exception);
                }
            }

            GetMetadata(fileItem);
            try
            {
                using (MagickImage image = new MagickImage(filename))
                {
                    fileItem.FileInfo.SetSize(image.Width, image.Height);

                    double dw = (double)LargeThumbSize / image.Width;
                    image.FilterType = FilterType.Box;
                    image.Thumbnail((int)(image.Width * dw), (int)(image.Height * dw));

                    if (!ServiceProvider.Settings.DisableHardwareAccelerationNew)
                    {
                        image.UnsharpMask(1, 1, 0.5, 0.1);
                    }

                    PhotoUtils.CreateFolder(fileItem.LargeThumb);
                    image.Write(fileItem.LargeThumb);
                    fileItem.IsLoaded = true;
                    fileItem.Loading  = false;

                    dw = (double)SmallThumbSize / image.Width;
                    image.Thumbnail((int)(image.Width * dw), (int)(image.Height * dw));

                    if (!ServiceProvider.Settings.DisableHardwareAccelerationNew)
                    {
                        image.UnsharpMask(1, 1, 0.5, 0.1);
                    }

                    PhotoUtils.CreateFolder(fileItem.SmallThumb);
                    image.Write(fileItem.SmallThumb);

                    fileItem.Thumbnail = LoadImage(fileItem.SmallThumb);
                }
                fileItem.SaveInfo();
                SetImageInfo(fileItem);
                if (deleteFile)
                {
                    File.Delete(filename);
                }
                OnMetaDataUpdated(fileItem);
            }
            catch (Exception exception)
            {
                Log.Error("Error generating cache " + fileItem.FileName, exception);
            }
            fileItem.Loading = false;
        }