Ejemplo n.º 1
0
        public static ImageInfo GetImageInformation(string fileName)
        {
            var result = new ImageInfo();

            result.Width  = 0;
            result.Height = 0;
            result.FormattedDimensions = "unknown";
            result.FormattedSize       = "unknown";
            result.SizeInBytes         = 0;

            var fullName = string.Empty;

            if (fileName != null)
            {
                fullName = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, fileName.Replace("/", "\\"));
            }


            if (File.Exists(fullName))
            {
                var f = new FileInfo(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, fileName));
                if (f != null)
                {
                    result.SizeInBytes = f.Length;
                    if (result.SizeInBytes < 1024)
                    {
                        result.FormattedSize = result.SizeInBytes + " bytes";
                    }
                    else
                    {
                        if (result.SizeInBytes < 1048576)
                        {
                            result.FormattedSize = Math.Round(result.SizeInBytes / 1024, 1) + " KB";
                        }
                        else
                        {
                            result.FormattedSize = Math.Round(result.SizeInBytes / 1048576, 1) + " MB";
                        }
                    }
                }
                f = null;

                if (File.Exists(fullName))
                {
                    Image WorkingImage;
                    WorkingImage = Image.FromFile(fullName);
                    if (WorkingImage != null)
                    {
                        result.Width  = WorkingImage.Width;
                        result.Height = WorkingImage.Height;
                        result.FormattedDimensions = result.Width.ToString(CultureInfo.InvariantCulture) + " x " +
                                                     result.Height.ToString(CultureInfo.InvariantCulture);
                    }
                    WorkingImage.Dispose();
                    WorkingImage = null;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static bool CompressJpeg(string filePath, long quality)
        {
            bool   result   = true;
            string fullFile = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, filePath);

            if (File.Exists(fullFile) == true)
            {
                if (quality < 1L)
                {
                    quality = 1L;
                }
                else
                {
                    if (quality > 100L)
                    {
                        quality = 100L;
                    }
                }

                System.Drawing.Image WorkingImage;
                WorkingImage = System.Drawing.Image.FromFile(filePath);

                System.Drawing.Image FinalImage;
                FinalImage = new System.Drawing.Bitmap(WorkingImage.Width, WorkingImage.Height, PixelFormat.Format24bppRgb);

                Graphics G = Graphics.FromImage(FinalImage);
                G.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                G.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                G.CompositingQuality = CompositingQuality.HighQuality;
                G.SmoothingMode      = SmoothingMode.HighQuality;
                G.DrawImage(WorkingImage, 0, 0, WorkingImage.Width, WorkingImage.Height);

                // Dispose working Image so we can save with the same name
                WorkingImage.Dispose();
                WorkingImage = null;

                // Compression Code
                ImageCodecInfo    myCodec         = GetEncoderInfo("image/jpeg");
                Encoder           myEncoder       = Encoder.Quality;
                EncoderParameters myEncoderParams = new EncoderParameters(1);
                EncoderParameter  myParam         = new EncoderParameter(myEncoder, quality);
                myEncoderParams.Param[0] = myParam;
                // End Compression Code

                File.Delete(fullFile);
                FinalImage.Save(fullFile, myCodec, myEncoderParams);
                FinalImage.Dispose();
                FinalImage = null;
            }
            else
            {
                result = false;
            }

            return(result);
        }