/// <summary>
        /// Creates a thumbnail of the specified image filename
        /// </summary>
        /// <param name="aInputFilename">The source filename to load a System.Drawing.Image from</param>
        /// <param name="aThumbTargetPath">Filename of the thumbnail to create</param>
        /// <param name="aThumbWidth">Maximum width of the thumbnail</param>
        /// <param name="aThumbHeight">Maximum height of the thumbnail</param>
        /// <param name="aRotation">
        /// 0 = no rotate
        /// 1 = rotate 90 degrees
        /// 2 = rotate 180 degrees
        /// 3 = rotate 270 degrees
        /// </param>
        /// <param name="aFastMode">Use low quality resizing without interpolation suitable for small thumbnails</param>
        /// <returns>Whether the thumb has been successfully created</returns>
        private static bool CreateThumbnail(string aInputFilename, string aThumbTargetPath,
                                            int iMaxWidth, int iMaxHeight, int iRotate)
        {
            if (string.IsNullOrEmpty(aInputFilename) || string.IsNullOrEmpty(aThumbTargetPath) || iMaxHeight <= 0 ||
                iMaxHeight <= 0)
            {
                return(false);
            }

            if (!File.Exists(aInputFilename))
            {
                return(false);
            }

            Image myImage = null;

            try
            {
                myImage = ImageFast.FromFile(aInputFilename);
                return(CreateThumbnail(myImage, aThumbTargetPath, iMaxWidth, iMaxHeight, iRotate, true));
            }
            catch (ArgumentException)
            {
                Log.Info("Picture: Fast loading of thumbnail {0} failed - trying safe fallback now", aInputFilename);

                try
                {
                    myImage = Image.FromFile(aInputFilename, true);
                    return(CreateThumbnail(myImage, aThumbTargetPath, iMaxWidth, iMaxHeight, iRotate, true));
                }
                catch (OutOfMemoryException)
                {
                    Log.Info("Picture: Creating thumbnail failed - image format is not supported of {0}", aInputFilename);
                    return(false);
                }
                catch (Exception ex)
                {
                    Log.Error("Picture: CreateThumbnail exception err:{0} stack:{1}", ex.Message, ex.StackTrace);
                    return(false);
                }
            }
            finally
            {
                if (myImage != null)
                {
                    myImage.Dispose();
                }
            }
        }
Beispiel #2
0
        public static bool CreateTileThumb(List <string> aPictureList, string aThumbPath, int PreviewColumns, int PreviewRows)
        {
            bool result = false;

            if (aPictureList.Count > 0)
            {
                try
                {
                    // Use first generated picture to take the ratio
                    Image imgFolder = null;
                    try
                    {
                        imgFolder = ImageFast.FromFile(aPictureList[0]);
                    }
                    catch (Exception)
                    {
                        Log.Debug("TvThumbnails: Fast loading failed {0} failed using safer fallback", aPictureList[0]);
                        imgFolder = Image.FromFile(aPictureList[0]);
                    }

                    if (imgFolder != null)
                    {
                        using (imgFolder)
                        {
                            int width  = imgFolder.Width;
                            int height = imgFolder.Height;

                            int thumbnailWidth  = 256;
                            int thumbnailHeight = 256;
                            // draw a fullsize thumb if only 1 pic is available
                            switch (PreviewColumns)
                            {
                            case 1:
                                thumbnailWidth = width;
                                break;

                            case 2:
                                thumbnailWidth = width / 2;
                                break;

                            case 3:
                                thumbnailWidth = width / 3;
                                break;
                            }
                            switch (PreviewRows)
                            {
                            case 1:
                                thumbnailHeight = height;
                                break;

                            case 2:
                                thumbnailHeight = height / 2;
                                break;

                            case 3:
                                thumbnailHeight = height / 3;
                                break;
                            }

                            using (Bitmap bmp = new Bitmap(width, height))
                            {
                                using (Graphics g = Graphics.FromImage(bmp))
                                {
                                    g.CompositingQuality = Thumbs.Compositing;
                                    g.InterpolationMode  = Thumbs.Interpolation;
                                    g.SmoothingMode      = Thumbs.Smoothing;

                                    g.DrawImage(imgFolder, 0, 0, width, height);
                                    int w, h;
                                    w = thumbnailWidth;
                                    h = thumbnailHeight;

                                    try
                                    {
                                        if (PreviewColumns == 1 && PreviewRows == 1)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                        }
                                        if (PreviewColumns == 1 && PreviewRows == 2)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], 0, h, w, h);
                                        }
                                        if (PreviewColumns == 2 && PreviewRows == 1)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], w, 0, w, h);
                                        }
                                        if (PreviewColumns == 2 && PreviewRows == 2)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], w, 0, w, h);
                                            AddPicture(g, (string)aPictureList[2], 0, h, w, h);
                                            AddPicture(g, (string)aPictureList[3], w, h, w, h);
                                        }
                                        if (PreviewColumns == 1 && PreviewRows == 3)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], 0, h, w, h);
                                            AddPicture(g, (string)aPictureList[2], 0, 2 * h, w, h);
                                        }
                                        if (PreviewColumns == 2 && PreviewRows == 3)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], w, 0, w, h);
                                            AddPicture(g, (string)aPictureList[2], 0, h, w, h);
                                            AddPicture(g, (string)aPictureList[3], w, h, w, h);
                                            AddPicture(g, (string)aPictureList[4], 0, 2 * h, w, h);
                                            AddPicture(g, (string)aPictureList[5], w, 2 * h, w, h);
                                        }
                                        if (PreviewColumns == 3 && PreviewRows == 3)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], w, 0, w, h);
                                            AddPicture(g, (string)aPictureList[2], 2 * w, 0, w, h);
                                            AddPicture(g, (string)aPictureList[3], 0, h, w, h);
                                            AddPicture(g, (string)aPictureList[4], w, h, w, h);
                                            AddPicture(g, (string)aPictureList[5], 2 * w, h, w, h);
                                            AddPicture(g, (string)aPictureList[6], 0, 2 * h, w, h);
                                            AddPicture(g, (string)aPictureList[7], w, 2 * h, w, h);
                                            AddPicture(g, (string)aPictureList[8], 2 * w, 2 * h, w, h);
                                        }
                                        if (PreviewColumns == 3 && PreviewRows == 1)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], w, 0, w, h);
                                            AddPicture(g, (string)aPictureList[2], 2 * w, 0, w, h);
                                        }
                                        if (PreviewColumns == 3 && PreviewRows == 2)
                                        {
                                            AddPicture(g, (string)aPictureList[0], 0, 0, w, h);
                                            AddPicture(g, (string)aPictureList[1], w, 0, w, h);
                                            AddPicture(g, (string)aPictureList[2], 2 * w, 0, w, h);
                                            AddPicture(g, (string)aPictureList[3], 0, h, w, h);
                                            AddPicture(g, (string)aPictureList[4], w, h, w, h);
                                            AddPicture(g, (string)aPictureList[5], 2 * w, h, w, h);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Log.Error("Utils: An exception occured creating CreateTileThumb: {0}", ex.Message);
                                    }
                                }

                                try
                                {
                                    string tmpFile = Path.GetTempFileName();
                                    Log.Debug("CreateTileThumb: before Saving thumb! tmpFile: {0}", tmpFile);

                                    bmp.Save(tmpFile, Thumbs.ThumbCodecInfo, Thumbs.ThumbEncoderParams);
                                    Log.Debug("CreateTileThumb: Saving thumb!");
                                    CreateThumbnail(tmpFile, aThumbPath, (int)Thumbs.ThumbLargeResolution,
                                                    (int)Thumbs.ThumbLargeResolution, 0);
                                    Log.Debug("CreateTileThumb: after Saving thumb!");

                                    File.Delete(tmpFile);

                                    if (imgFolder != null)
                                    {
                                        imgFolder.Dispose();
                                    }

                                    if (aPictureList.Count > 0)
                                    {
                                        string pictureListName = string.Empty;
                                        try
                                        {
                                            for (int i = 0; i < (aPictureList.Count); i++)
                                            {
                                                pictureListName = aPictureList[i];
                                                File.Delete(aPictureList[i]);
                                            }
                                        }
                                        catch (FileNotFoundException)
                                        {
                                            Log.Debug("CreateTileThumb: {0} file not found.", pictureListName);
                                        }
                                    }

                                    Thread.Sleep(100);

                                    if (File.Exists(aThumbPath))
                                    {
                                        result = true;
                                    }
                                }
                                catch (Exception ex2)
                                {
                                    Log.Error("CreateTileThumb: An exception occured saving CreateTileThumb: {0} - {1}", aThumbPath,
                                              ex2.Message);
                                }
                            }
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    Log.Error("CreateTileThumb: Your skin does not find first image to create CreateTileThumb!");
                }
                catch (Exception exm)
                {
                    Log.Error("CreateTileThumb: An error occured creating folder CreateTileThumb: {0}", exm.Message);
                }
            }
            return(result);
        }