Example #1
0
        public static Image SaveThumbnailImage(Image source, int targetWidth, int targetHeight, ShearType shearType = ShearType.None)
        {
            if (shearType == ShearType.None)
            {
                float num1   = targetWidth / (float)targetHeight;
                float num2   = source.Width / (float)source.Height;
                int   width  = source.Width;
                int   height = source.Height;
                if (width > targetWidth || height > targetHeight)
                {
                    if (num2 > (double)num1)
                    {
                        width  = targetWidth;
                        height = Convert.ToInt32(targetWidth / num2);
                    }
                    else
                    {
                        width  = Convert.ToInt32(targetHeight * num2);
                        height = targetHeight;
                    }
                }
                Image    image1      = new Bitmap(width, height);
                Graphics graphics    = Graphics.FromImage(image1);
                Color    transparent = Color.Transparent;
                graphics.Clear(transparent);
                int num3 = 2;
                graphics.CompositingQuality = (CompositingQuality)num3;
                int num4 = 7;
                graphics.InterpolationMode = (InterpolationMode)num4;
                int num5 = 2;
                graphics.SmoothingMode = (SmoothingMode)num5;
                Image     image2   = source;
                Rectangle destRect = new Rectangle(0, 0, image1.Width, image1.Height);
                Rectangle srcRect  = new Rectangle(0, 0, source.Width, source.Height);
                int       num6     = 2;
                graphics.DrawImage(image2, destRect, srcRect, (GraphicsUnit)num6);
                graphics.Dispose();
                return(image1);
            }
            float num7 = source.Height / (float)targetHeight;
            float num8 = source.Width / (float)targetWidth;
            int   x;
            int   width1;
            int   height1;
            int   y;

            if (num8 < (double)num7)
            {
                x       = 0;
                width1  = source.Width;
                height1 = (int)(num8 * (double)targetHeight);
                y       = (source.Height - height1) / 2;
            }
            else
            {
                y       = 0;
                height1 = source.Height;
                width1  = (int)(targetWidth * (double)num7);
                x       = (source.Width - width1) / 2;
            }
            Image    image3       = new Bitmap(targetWidth, targetHeight);
            Graphics graphics1    = Graphics.FromImage(image3);
            Color    transparent1 = Color.Transparent;

            graphics1.Clear(transparent1);
            int num9 = 2;

            graphics1.CompositingQuality = (CompositingQuality)num9;
            int num10 = 7;

            graphics1.InterpolationMode = (InterpolationMode)num10;
            int num11 = 2;

            graphics1.SmoothingMode = (SmoothingMode)num11;
            Image     image4    = source;
            Rectangle destRect1 = new Rectangle(0, 0, image3.Width, image3.Height);
            Rectangle srcRect1  = new Rectangle(x, y, width1, height1);
            int       num12     = 2;

            graphics1.DrawImage(image4, destRect1, srcRect1, (GraphicsUnit)num12);
            graphics1.Dispose();
            return(image3);
        }
Example #2
0
 public static Size SaveThumbnailImage(Image source, string targetFileName, int targetWidth, int targetHeight, ShearType shearType = ShearType.None)
 {
     using (Image srcImg = SaveThumbnailImage(source, targetWidth, targetHeight, shearType))
     {
         Size size = new Size(srcImg.Width, srcImg.Height);
         Compress(srcImg, targetFileName, 80L);
         return(size);
     }
 }
Example #3
0
 public static Size SaveThumbnailImage(Stream sourceStream, string targetFileName, int targetWidth, int targetHeight, ShearType shearType = ShearType.None)
 {
     using (Image source = Image.FromStream(sourceStream))
     {
         Size size = SaveThumbnailImage(source, targetFileName, targetWidth, targetHeight, shearType);
         source.Dispose();
         return(size);
     }
 }
Example #4
0
 public static Size SaveThumbnailImage(string sourceFileName, string targetFileName, int targetWidth, int targetHeight, ShearType shearType = ShearType.None)
 {
     using (Stream stream = File.OpenRead(sourceFileName))
     {
         Image  source          = Image.FromStream(stream);
         string targetFileName1 = targetFileName;
         int    targetWidth1    = targetWidth;
         int    targetHeight1   = targetHeight;
         int    num             = (int)shearType;
         Size   size            = SaveThumbnailImage(source, targetFileName1, targetWidth1, targetHeight1, (ShearType)num);
         source.Dispose();
         stream.Close();
         return(size);
     }
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="oImage"></param>
        /// <param name="nShearType"></param>
        /// <param name="BackColour"></param>
        /// <param name="nAmount"></param>
        /// <param name="bResize"></param>
        /// <returns></returns>
        public static Bitmap ShearImage(Image oImage, ShearType nShearType, Color BackColour, int nAmount, bool bResize)
        {
            if (oImage == null)
                return null;

            int nW = oImage.Width;
            int nH = oImage.Height;
            Point[] Points = new Point[3];

            Points[0] = new Point((nShearType == ShearType.Horizontal) ? ((nAmount >= 0) ? nAmount : 0) : 0,
                (nShearType == ShearType.Vertical) ? ((nAmount < 0) ? Math.Abs(nAmount) : 0) : 0);
            Points[1] = new Point((nShearType == ShearType.Horizontal) ? ((nAmount >= 0) ? nW + nAmount : nW) : nW,
                (nShearType == ShearType.Vertical) ? ((nAmount >= 0) ? nAmount : 0) : 0);
            Points[2] = new Point((nShearType == ShearType.Horizontal) ? ((nAmount < 0) ? Math.Abs(nAmount) : 0) : 0,
                (nShearType == ShearType.Vertical) ? ((nAmount < 0) ? nH + Math.Abs(nAmount) : nH) : nH);

            nW = bResize ? ((nShearType == ShearType.Horizontal) ? nW + Math.Abs(nAmount) : nW) : nW;
            nH = bResize ? ((nShearType == ShearType.Vertical) ? nH + Math.Abs(nAmount) : nH) : nH;
            
            using (Bitmap oBitmap = new Bitmap(nW, nH))
            {
                using (Graphics oGraphics = Graphics.FromImage(oBitmap))
                {
                    using (SolidBrush oBrush = new SolidBrush(BackColour))
                        oGraphics.FillRectangle(oBrush, new Rectangle(Point.Empty, oBitmap.Size));

                    oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
                    oGraphics.CompositingQuality = CompositingQuality.HighQuality;
                    oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    oGraphics.DrawImage(oImage, Points);
                }

                return new Bitmap(oBitmap, oBitmap.Size);
            }
        }