예제 #1
0
        /// <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>
        public static bool CreateThumbnail(string aInputFilename, string aThumbTargetPath, int iMaxWidth, int iMaxHeight,
                                           int iRotate, bool aFastMode)
        {
            bool result = false;

            if (string.IsNullOrEmpty(aInputFilename) || string.IsNullOrEmpty(aThumbTargetPath) || iMaxHeight <= 0 ||
                iMaxHeight <= 0)
            {
                return(false);
            }

            Image myImage = null;

            try
            {
                //if (aFastMode)
                //{
                //  try
                //  {
                //    Extractor.DesiredSize = new Size(iMaxWidth, iMaxHeight);
                //    myImage = Extractor.GetThumbnail(aInputFilename);
                //    return SaveThumbnail(aThumbTargetPath, myImage);
                //  }
                //  catch (Exception) { }
                //}
                try
                {
                    myImage = ImageFast.FromFile(aInputFilename);
                }
                catch (FileNotFoundException)
                {
                    result = false;
                }

                result = CreateThumbnail(myImage, aThumbTargetPath, iMaxWidth, iMaxHeight, iRotate, aFastMode);
            }
            catch (Exception)
            {
                Log.Warn("Picture: Fast loading of thumbnail {0} failed - trying safe fallback now", aInputFilename);

                try
                {
                    myImage = Image.FromFile(aInputFilename, true);
                    result  = CreateThumbnail(myImage, aThumbTargetPath, iMaxWidth, iMaxHeight, iRotate, aFastMode);
                }
                catch (FileNotFoundException)
                {
                    result = false;
                }
                catch (OutOfMemoryException)
                {
                    Log.Warn("Picture: Creating thumbnail failed - image format is not supported of {0}", aInputFilename);
                    result = false;
                }
                catch (Exception ex)
                {
                    Log.Error("Picture: CreateThumbnail exception err:{0} stack:{1}", ex.Message, ex.StackTrace);
                    result = false;
                }
            }
            finally
            {
                if (myImage != null)
                {
                    myImage.SafeDispose();
                }
            }

            return(result);
        }
예제 #2
0
        public static Texture Load(string strPic, int iRotate, int iMaxWidth, int iMaxHeight, bool bRGB, bool bZoom,
                                   bool bOversized, out int iWidth, out int iHeight)
        {
            iWidth  = 0;
            iHeight = 0;
            if (strPic == null)
            {
                return(null);
            }
            if (strPic == string.Empty)
            {
                return(null);
            }

            Direct3D.Texture texture  = null;
            Image            theImage = null;

            try
            {
                try
                {
                    theImage = ImageFast.FromFile(strPic);
                    Log.Debug("Picture: Fast loaded texture {0}", strPic);
                }
                catch (Exception)
                {
                    theImage = Image.FromFile(strPic);
                    Log.Warn("Picture: Fallback loaded texture {0}", strPic);
                }
                if (theImage == null)
                {
                    return(null);
                }
                if (iRotate > 0)
                {
                    RotateFlipType fliptype;
                    switch (iRotate)
                    {
                    case 1:
                        fliptype = RotateFlipType.Rotate90FlipNone;
                        theImage.RotateFlip(fliptype);
                        break;

                    case 2:
                        fliptype = RotateFlipType.Rotate180FlipNone;
                        theImage.RotateFlip(fliptype);
                        break;

                    case 3:
                        fliptype = RotateFlipType.Rotate270FlipNone;
                        theImage.RotateFlip(fliptype);
                        break;

                    default:
                        fliptype = RotateFlipType.RotateNoneFlipNone;
                        break;
                    }
                }
                iWidth  = theImage.Size.Width;
                iHeight = theImage.Size.Height;

                int iBitmapWidth  = iWidth;
                int iBitmapHeight = iHeight;

                bool  bResize = false;
                float fOutputFrameAR;
                if (bZoom)
                {
                    bResize       = true;
                    iBitmapWidth  = iMaxWidth;
                    iBitmapHeight = iMaxHeight;
                    while (iWidth < iMaxWidth || iHeight < iMaxHeight)
                    {
                        iWidth  *= 2;
                        iHeight *= 2;
                    }
                    int   iOffsetX1      = GUIGraphicsContext.OverScanLeft;
                    int   iOffsetY1      = GUIGraphicsContext.OverScanTop;
                    int   iScreenWidth   = GUIGraphicsContext.OverScanWidth;
                    int   iScreenHeight  = GUIGraphicsContext.OverScanHeight;
                    float fPixelRatio    = GUIGraphicsContext.PixelRatio;
                    float fSourceFrameAR = ((float)iWidth) / ((float)iHeight);
                    fOutputFrameAR = fSourceFrameAR / fPixelRatio;
                }
                else
                {
                    fOutputFrameAR = ((float)iWidth) / ((float)iHeight);
                }

                if (iWidth > iMaxWidth)
                {
                    bResize = true;
                    iWidth  = iMaxWidth;
                    iHeight = (int)(((float)iWidth) / fOutputFrameAR);
                }

                if (iHeight > (int)iMaxHeight)
                {
                    bResize = true;
                    iHeight = iMaxHeight;
                    iWidth  = (int)(fOutputFrameAR * ((float)iHeight));
                }

                if (!bOversized)
                {
                    iBitmapWidth  = iWidth;
                    iBitmapHeight = iHeight;
                }
                else
                {
                    // Adjust width/height 2 pixcels for smoother zoom actions at the edges
                    iBitmapWidth  = iWidth + 2;
                    iBitmapHeight = iHeight + 2;
                    bResize       = true;
                }

                if (bResize)
                {
                    using (Bitmap result = new Bitmap(iBitmapWidth, iBitmapHeight))
                    {
                        using (Graphics g = Graphics.FromImage(result))
                        {
                            g.CompositingQuality = Thumbs.Compositing;
                            g.InterpolationMode  = Thumbs.Interpolation;
                            g.SmoothingMode      = Thumbs.Smoothing;
                            if (bOversized)
                            {
                                // Set picture at center position
                                int xpos = 1; // (iMaxWidth-iWidth)/2;
                                int ypos = 1; // (iMaxHeight-iHeight)/2;
                                g.DrawImage(theImage, new Rectangle(xpos, ypos, iWidth, iHeight));
                            }
                            else
                            {
                                g.DrawImage(theImage, new Rectangle(0, 0, iWidth, iHeight));
                            }
                        }
                        texture = Picture.ConvertImageToTexture(result, out iWidth, out iHeight);
                    }
                }
                else
                {
                    texture = Picture.ConvertImageToTexture((Bitmap)theImage, out iWidth, out iHeight);
                }
            }
            catch (ThreadAbortException ext)
            {
                Log.Debug("Picture: exception loading {0} err:{1}", strPic, ext.Message);
            }
            catch (Exception ex)
            {
                Log.Warn("Picture: exception loading {0} err:{1}", strPic, ex.Message);
            }
            finally
            {
                if (theImage != null)
                {
                    theImage.SafeDispose();
                }
            }
            return(texture);
        }