Пример #1
0
        /// <summary>
        /// Save specified thumb
        /// </summary>
        public void SaveThumb(ThumbType thumbType)
        {
            if (ID < -1)
            {
                //new Emulator, no save directory
                return;
            }

            string friendlyName = null;
            string fileName     = null;
            Image  thumb        = null;

            bool resize = false;

            string savePath = ThumbPath;

            //check and create thumb directory
            if (!System.IO.Directory.Exists(savePath))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(savePath);
                }
                catch (Exception ex)
                {
                    Logger.LogError("Error creating thumb directory for {0} - {1}", ParentTitle, ex.Message);
                    return;
                }
            }

            //get file/friendly name and set resize option
            switch (thumbType)
            {
            case ThumbType.FrontCover:
                if (parentItemIsGame)
                {
                    friendlyName = "Front Cover";
                    savePath    += BOX_FRONT_NAME;
                    resize       = Options.Instance.GetBoolOption("resizethumbs");
                }
                else
                {
                    //Emulator so set front cover to Logo
                    friendlyName = "Logo";
                    savePath    += LOGO_NAME;
                }

                fileName = frontCover.Path;
                thumb    = frontCover.Image;
                break;

            case ThumbType.BackCover:
                friendlyName = "Back Cover";
                savePath    += BOX_BACK_NAME;
                resize       = Options.Instance.GetBoolOption("resizethumbs");

                fileName = backCover.Path;
                thumb    = backCover.Image;
                break;

            case ThumbType.TitleScreen:
                friendlyName = "Title Screen";
                savePath    += TITLESCREEN_NAME;

                fileName = titleScreen.Path;
                thumb    = titleScreen.Image;
                break;

            case ThumbType.InGameScreen:
                friendlyName = "In Game Screen";
                savePath    += INGAME_NAME;

                fileName = inGame.Path;
                thumb    = inGame.Image;
                break;

            case ThumbType.Fanart:
                friendlyName = "Fanart";
                savePath    += FANART_NAME;

                fileName = fanart.Path;
                thumb    = fanart.Image;
                break;
            }

            if (thumb == null)
            {
                //only delete if filename is also empty, else there was a problem
                //loading the image but path may still be valid
                if (fileName == "")
                {
                    //Delete thumb
                    try
                    {
                        System.IO.File.Delete(savePath + ".jpg");
                    }
                    catch { }
                    try
                    {
                        System.IO.File.Delete(savePath + ".png");
                    }
                    catch { }
                }
                else
                {
                    Logger.LogError("Unable to save {0} for {1} - error loading path '{2}'", friendlyName, ParentTitle, fileName);
                }
                return;
            }

            bool shrinkThumb       = false;
            int  maxThumbDimension = 0;

            if (thumbType != ThumbType.Fanart)
            {
                maxThumbDimension = Options.Instance.GetIntOption("maxthumbdimension");
                if (maxThumbDimension < 0)
                {
                    maxThumbDimension = 0;
                }

                shrinkThumb = maxThumbDimension > 0 && (thumb.Width > maxThumbDimension || thumb.Height > maxThumbDimension);
            }


            string ext = ".png";

            //if we are not resizing and we have a reference to an image in a valid format
            //on the local file system, copy the file to the thumb location
            if (!resize && !shrinkThumb && isValidThumbPath(fileName, ref ext))
            {
                if (fileName != savePath + ext) //check that image actually needs updating
                {
                    try
                    {
                        System.IO.File.Copy(fileName, savePath + ext, true);
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError("Error copying new {0} for {1} - {2}", friendlyName, ParentTitle, ex.Message);
                        return;
                    }
                }
            }
            else
            {
                //If first save set image encoder parameters
                if (jpegCodec == null)
                {
                    initImageEncoder();
                }
                try
                {
                    if (resize || shrinkThumb)
                    {
                        thumb = ImageHandler.ResizeImage(thumb, resize ? thumbaspect : 0, shrinkThumb ? maxThumbDimension : 0);
                    }
                    //save image to thumb location
                    thumb.Save(savePath + ext, jpegCodec, encoderParams);
                }
                catch (Exception ex)
                {
                    Logger.LogError("Error saving new {0} for {1} - {2}", friendlyName, ParentTitle, ex.Message);
                    return;
                }
            }

            //if image is jpg, remove any png's and vice versa
            RemoveAlternateThumb(savePath + ext);

            //update thumb path to save location
            switch (thumbType)
            {
            case ThumbType.FrontCover:
                frontCover.Path = savePath + ext;
                break;

            case ThumbType.BackCover:
                backCover.Path = savePath + ext;
                break;

            case ThumbType.TitleScreen:
                titleScreen.Path = savePath + ext;
                break;

            case ThumbType.InGameScreen:
                inGame.Path = savePath + ext;
                break;

            case ThumbType.Fanart:
                fanart.Path = savePath + ext;
                break;
            }
        }