Ejemplo n.º 1
0
        public static bool MakeThumbnailImage(
            string fileName,
            string newFileName,
            int maxWidth,
            int maxHeight,
            int cropWidth,
            int cropHeight,
            int X,
            int Y)
        {
            Image  image  = Image.FromStream((Stream) new MemoryStream(System.IO.File.ReadAllBytes(fileName)));
            Bitmap bitmap = new Bitmap(cropWidth, cropHeight);

            try
            {
                using (Graphics graphics = Graphics.FromImage((Image)bitmap))
                {
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode     = SmoothingMode.AntiAlias;
                    graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                    graphics.Clear(Color.Transparent);
                    graphics.DrawImage(image, new Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel);
                    Thumbnail.SaveImage((Image) new Bitmap((Image)bitmap, maxWidth, maxHeight), newFileName, Thumbnail.GetCodecInfo("image/" + Thumbnail.GetFormat(newFileName).ToString().ToLower()));
                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
            }
        }
Ejemplo n.º 2
0
 public static void MakeSquareImage(string fileName, string newFileName, int newSize)
 {
     Thumbnail.MakeSquareImage(Image.FromFile(fileName), newFileName, newSize);
 }
Ejemplo n.º 3
0
        public static void MakeThumbnailImage(
            string fileName,
            string newFileName,
            int width,
            int height,
            string mode)
        {
            Image image   = Image.FromFile(fileName);
            int   width1  = width;
            int   height1 = height;
            int   x       = 0;
            int   y       = 0;
            int   width2  = image.Width;
            int   height2 = image.Height;

            switch (mode)
            {
            case "HW":
                if ((double)image.Width / (double)image.Height > (double)width1 / (double)height1)
                {
                    width2  = image.Width;
                    height2 = image.Width * height / width1;
                    x       = 0;
                    y       = (image.Height - height2) / 2;
                    break;
                }
                height2 = image.Height;
                width2  = image.Height * width1 / height1;
                y       = 0;
                x       = (image.Width - width2) / 2;
                break;

            case "W":
                height1 = image.Height * width / image.Width;
                break;

            case "H":
                width1 = image.Width * height / image.Height;
                break;

            case "Cut":
                if ((double)image.Width / (double)image.Height > (double)width1 / (double)height1)
                {
                    height2 = image.Height;
                    width2  = image.Height * width1 / height1;
                    y       = 0;
                    x       = (image.Width - width2) / 2;
                    break;
                }
                width2  = image.Width;
                height2 = image.Width * height / width1;
                x       = 0;
                y       = (image.Height - height2) / 2;
                break;
            }
            Bitmap bitmap = new Bitmap(width1, height1);

            try
            {
                Graphics graphics = Graphics.FromImage((Image)bitmap);
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode     = SmoothingMode.AntiAlias;
                graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                graphics.Clear(Color.White);
                graphics.DrawImage(image, new Rectangle(0, 0, width1, height1), new Rectangle(x, y, width2, height2), GraphicsUnit.Pixel);
                Thumbnail.SaveImage((Image)bitmap, newFileName, Thumbnail.GetCodecInfo("image/" + Thumbnail.GetFormat(newFileName).ToString().ToLower()));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
            }
        }
Ejemplo n.º 4
0
        public static void MakeSquareImage(Image image, string newFileName, int newSize)
        {
            int num    = 0;
            int width  = image.Width;
            int height = image.Height;

            num = width <= height ? width : height;
            Bitmap bitmap = new Bitmap(newSize, newSize);

            try
            {
                Graphics graphics = Graphics.FromImage((Image)bitmap);
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode     = SmoothingMode.AntiAlias;
                graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                graphics.Clear(Color.Transparent);
                if (width < height)
                {
                    graphics.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle(0, (height - width) / 2, width, width), GraphicsUnit.Pixel);
                }
                else
                {
                    graphics.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle((width - height) / 2, 0, height, height), GraphicsUnit.Pixel);
                }
                Thumbnail.SaveImage((Image)bitmap, newFileName, Thumbnail.GetCodecInfo("image/" + Thumbnail.GetFormat(newFileName).ToString().ToLower()));
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
            }
        }