示例#1
0
 /// <summary>
 /// 获取指定文件的等比例缩略图。
 /// </summary>
 /// <param name="FileName">图片文件全名。</param>
 /// <param name="Width">缩略图宽度。</param>
 /// <param name="Height">缩略图高度。</param>
 /// <param name="Proportion">缩放时维持等比例。</param>
 /// <param name="LockSmallImage">锁定小尺寸图片。如果此项设置为 true,当原图像尺寸小于缩略图限制尺寸时返回原图像尺寸。</param>
 /// <param name="ImageQuality">输出图片质量。</param>
 /// <param name="KeepWhite">指示是否保留空白边距。如果保留空白边距则使用指定的颜色填充,以确保输出的图像的尺寸与指定的尺寸相等。否则输出的图像的尺寸可能小于指定的尺寸。</param>
 /// <param name="WhiteFillColor">空白区域填充颜色。</param>
 /// <returns>一个 System.Drawing.Image 实例。</returns>
 public static System.Drawing.Bitmap GetThumbnailImage(string FileName, int Width, int Height, bool Proportion, bool LockSmallImage, Thinksea.eImageQuality ImageQuality, bool KeepWhite, System.Drawing.Color WhiteFillColor)
 {
     System.Drawing.Image img = Thinksea.Image.GetImageFromFile(FileName);
     try
     {
         return(Thinksea.Image.GetThumbnailImage(img, Width, Height, Proportion, LockSmallImage, ImageQuality, KeepWhite, WhiteFillColor));
     }
     finally
     {
         img.Dispose();
     }
 }
示例#2
0
        /// <summary>
        /// 获取指定图像的等比例缩略图。
        /// </summary>
        /// <param name="image">用于提供图像数据的 System.Drawing.Image 实例。</param>
        /// <param name="Width">缩略图宽度。</param>
        /// <param name="Height">缩略图高度。</param>
        /// <param name="Proportion">缩放时维持等比例。</param>
        /// <param name="LockSmallImage">锁定小尺寸图片。如果此项设置为 true,当原图像尺寸小于缩略图限制尺寸时返回原图像尺寸。</param>
        /// <param name="ImageQuality">输出图片质量。</param>
        /// <param name="KeepWhite">指示是否保留空白边距。如果保留空白边距则使用指定的颜色填充,以确保输出的图像的尺寸与指定的尺寸相等。否则输出的图像的尺寸可能小于指定的尺寸。</param>
        /// <param name="WhiteFillColor">空白区域填充颜色。</param>
        /// <returns>一个 System.Drawing.Image 实例。</returns>
        public static System.Drawing.Bitmap GetThumbnailImage(System.Drawing.Image image, int Width, int Height, bool Proportion, bool LockSmallImage, Thinksea.eImageQuality ImageQuality, bool KeepWhite, System.Drawing.Color WhiteFillColor)
        {
            if (image == null)
            {
                throw new System.ArgumentNullException("image");
            }

            System.Drawing.RectangleF ImageRectangleF;
            if (Proportion || (LockSmallImage && KeepWhite))
            {
                ImageRectangleF = Thinksea.Image.GetThumbnailImageSize(image.Width, image.Height, Width, Height, LockSmallImage);
            }
            else
            {
                ImageRectangleF = new System.Drawing.RectangleF(0, 0, Width, Height);
                if (LockSmallImage)
                {
                    if (image.Width < Width)
                    {
                        ImageRectangleF.Width = image.Width;
                    }
                    if (image.Height < Height)
                    {
                        ImageRectangleF.Height = image.Height;
                    }
                }
            }

            System.Drawing.Imaging.PixelFormat outputFormat;

            System.Drawing.Bitmap bitmap = null;

            if (Image.IsPixelFormatIndexed(image.PixelFormat))
            {
                outputFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
                if (KeepWhite)
                {
                    bitmap = new System.Drawing.Bitmap(Width, Height, outputFormat);
                }
                else
                {
                    bitmap = new System.Drawing.Bitmap(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height), outputFormat);
                }
            }
            else
            {
                switch (image.PixelFormat)
                {
                case System.Drawing.Imaging.PixelFormat.Format48bppRgb:
                case System.Drawing.Imaging.PixelFormat.Format64bppPArgb:
                case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
                    outputFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
                    break;

                default:
                    outputFormat = image.PixelFormat;
                    break;
                }
                if (KeepWhite)
                {
                    bitmap = new System.Drawing.Bitmap(Width, Height, outputFormat);
                }
                else
                {
                    bitmap = new System.Drawing.Bitmap(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height), outputFormat);
                }
            }

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
            {
                switch (ImageQuality)
                {
                case Thinksea.eImageQuality.High:
                {
                    bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                }
                break;

                case Thinksea.eImageQuality.Low:
                {
                    //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height));
                    //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                    //g.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);

                    bitmap.SetResolution((image.HorizontalResolution > 96.0F ? 96.0F : image.HorizontalResolution), (image.VerticalResolution > 96.0F ? 96.0F : image.VerticalResolution));
                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.None;
                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.Low;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
                }
                break;

                case Thinksea.eImageQuality.Web:
                {
                    //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height));
                    //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                    //g.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);

                    bitmap.SetResolution((image.HorizontalResolution > 72.0F ? 72.0F : image.HorizontalResolution), (image.VerticalResolution > 72.0F ? 72.0F : image.VerticalResolution));
                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.None;
                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.Low;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
                }
                break;
                }

                if (KeepWhite)
                {
                    if (WhiteFillColor != System.Drawing.Color.Transparent)
                    {
                        g.Clear(WhiteFillColor);
                    }
                    g.DrawImage(image, ImageRectangleF);
                }
                else
                {
                    g.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);
                }
            }

            //switch (ImageQuality)
            //{
            //    case Thinksea.eImageQuality.High:
            //        {
            //        }
            //        break;
            //    case Thinksea.eImageQuality.Low:
            //        {
            //            //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height));
            //            //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //            //g.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);

            //            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            //            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
            //            //g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;



            //            bitmap = new System.Drawing.Bitmap(image.GetThumbnailImage(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height), delegate()
            //            {
            //                return false;
            //            }, System.IntPtr.Zero));
            //        }
            //        break;
            //}
            return(bitmap);
        }
示例#3
0
        /// <summary>
        /// 获取指定图像的等比例缩略图。
        /// </summary>
        /// <param name="image">用于提供图像数据的 System.Drawing.Image 实例。</param>
        /// <param name="width">缩略图宽度。取值为 0 时表示忽略此参数。</param>
        /// <param name="height">缩略图高度。取值为 0 时表示忽略此参数。</param>
        /// <param name="proportion">缩放时维持等比例。</param>
        /// <param name="lockSmallImage">锁定小尺寸图片。如果此项设置为 true,当原图像尺寸小于缩略图限制尺寸时返回原图像尺寸。</param>
        /// <param name="imageQuality">输出图片质量。</param>
        /// <param name="keepWhite">指示是否保留空白边距。如果保留空白边距则使用指定的颜色填充,以确保输出的图像的尺寸与指定的尺寸相等。否则输出的图像的尺寸可能小于指定的尺寸。</param>
        /// <param name="whiteFillColor">空白区域填充颜色。</param>
        /// <returns>一个 System.Drawing.Image 实例。</returns>
        public static System.Drawing.Bitmap GetThumbnailImage(System.Drawing.Image image, int width, int height, bool proportion, bool lockSmallImage, Thinksea.eImageQuality imageQuality, bool keepWhite, System.Drawing.Color whiteFillColor)
        {
            if (image == null)
            {
                throw new System.ArgumentNullException(nameof(image));
            }

            System.Drawing.RectangleF ImageRectangleF;
            if (width > 0 && height > 0 && proportion == false && keepWhite == true)
            {
                ImageRectangleF = new System.Drawing.RectangleF(0, 0, width, height);
                if (lockSmallImage)
                {
                    if (image.Width < width)
                    {
                        ImageRectangleF.Width = image.Width;
                        ImageRectangleF.X     = (width - image.Width) / 2.0F;
                    }
                    if (image.Height < height)
                    {
                        ImageRectangleF.Height = image.Height;
                        ImageRectangleF.Y      = (height - image.Height) / 2.0F;
                    }
                }
            }
            else
            {
                ImageRectangleF = Thinksea.Image.GetThumbnailImageSize(image.Width, image.Height, width, height, lockSmallImage);
                if (width == 0)
                {
                    width = (int)System.Math.Floor(ImageRectangleF.Right);
                }
                if (height == 0)
                {
                    height = (int)System.Math.Floor(ImageRectangleF.Bottom);
                }
            }

            System.Drawing.Imaging.PixelFormat outputFormat;

            System.Drawing.Bitmap bitmap;

            if (Image.IsPixelFormatIndexed(image.PixelFormat))
            {
                outputFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
                bitmap       = new System.Drawing.Bitmap(width, height, outputFormat);
            }
            else
            {
                switch (image.PixelFormat)
                {
                case System.Drawing.Imaging.PixelFormat.Format48bppRgb:
                case System.Drawing.Imaging.PixelFormat.Format64bppPArgb:
                case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
                    outputFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
                    break;

                default:
                    outputFormat = image.PixelFormat;
                    break;
                }
                bitmap = new System.Drawing.Bitmap(width, height, outputFormat);
            }

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
            {
                switch (imageQuality)
                {
                case Thinksea.eImageQuality.High:
                {
                    bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                }
                break;

                case Thinksea.eImageQuality.Low:
                {
                    //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height));
                    //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                    //g.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);

                    bitmap.SetResolution((image.HorizontalResolution > 96.0F ? 96.0F : image.HorizontalResolution), (image.VerticalResolution > 96.0F ? 96.0F : image.VerticalResolution));
                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.None;
                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.Low;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
                }
                break;

                case Thinksea.eImageQuality.Web:
                {
                    //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(System.Convert.ToInt32(ImageRectangleF.Width == 0 ? 1 : ImageRectangleF.Width), System.Convert.ToInt32(ImageRectangleF.Height == 0 ? 1 : ImageRectangleF.Height));
                    //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                    //g.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);

                    bitmap.SetResolution((image.HorizontalResolution > 72.0F ? 72.0F : image.HorizontalResolution), (image.VerticalResolution > 72.0F ? 72.0F : image.VerticalResolution));
                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.None;
                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.Low;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
                }
                break;
                }

                if (keepWhite && whiteFillColor != System.Drawing.Color.Transparent)
                {
                    g.Clear(whiteFillColor);
                }
                g.DrawImage(image, ImageRectangleF);
            }

            return(bitmap);
        }
示例#4
0
 /// <summary>
 /// 获取指定文件的等比例缩略图。
 /// </summary>
 /// <param name="fileName">图片文件全名。</param>
 /// <param name="width">缩略图宽度。取值为 0 时表示忽略此参数。</param>
 /// <param name="height">缩略图高度。取值为 0 时表示忽略此参数。</param>
 /// <param name="proportion">缩放时维持等比例。</param>
 /// <param name="lockSmallImage">锁定小尺寸图片。如果此项设置为 true,当原图像尺寸小于缩略图限制尺寸时返回原图像尺寸。</param>
 /// <param name="imageQuality">输出图片质量。</param>
 /// <param name="keepWhite">指示是否保留空白边距。如果保留空白边距则使用指定的颜色填充,以确保输出的图像的尺寸与指定的尺寸相等。否则输出的图像的尺寸可能小于指定的尺寸。</param>
 /// <param name="whiteFillColor">空白区域填充颜色。</param>
 /// <returns>一个 System.Drawing.Image 实例。</returns>
 public static System.Drawing.Bitmap GetThumbnailImage(string fileName, int width, int height, bool proportion, bool lockSmallImage, Thinksea.eImageQuality imageQuality, bool keepWhite, System.Drawing.Color whiteFillColor)
 {
     using (System.Drawing.Image img = Thinksea.Image.GetImageFromFile(fileName))
     {
         return(Thinksea.Image.GetThumbnailImage(img, width, height, proportion, lockSmallImage, imageQuality, keepWhite, whiteFillColor));
     }
 }
示例#5
0
        private void Run()
        {
            try
            {
                bool 保持原图比例  = (bool)this.GetPropertyValue(this.cb保持原图比例, "Checked");
                bool 保持小图尺寸  = (bool)this.GetPropertyValue(this.cb保持小图尺寸, "Checked");
                bool 转换为统一格式 = (bool)this.GetPropertyValue(this.cb转换为统一格式, "Checked");
                Thinksea.eImageQuality 图片质量   = (Thinksea.eImageQuality) this.GetPropertyValue(this.cmb输出图片质量, "SelectedValue");
                ImageFormat            中的统一格式 = (ImageFormat)this.GetPropertyValue(this.cmb存盘格式, "SelectedItem");

                int maxWidth = 0;
                if ((bool)this.GetPropertyValue(this.cb最大宽度, "Checked"))
                {
                    maxWidth = System.Convert.ToInt32(this.GetPropertyValue(this.nud最大宽度, "Value"));
                }
                int maxHeight = 0;
                if ((bool)this.GetPropertyValue(this.cb最大高度, "Checked"))
                {
                    maxHeight = System.Convert.ToInt32(this.GetPropertyValue(this.nud最大高度, "Value"));
                }

                string[] files = this.GetFiles();
                this.SetPropertyValue(this.toolStripProgressBar1, "Maximum", files.Length);

                for (int i = 0; i < files.Length && this.请求中止全部任务 == false; i++)
                {
                    string file = files[i];
                    this.SetPropertyValue(this.toolStripProgressBar1, "Value", i);
                    //this.SetPropertyValue(this.toolStripStatusLabel正在处理的文件, "Text", file);
                    this.正在处理的文件 = file;
                    string outputFile = file;
                    System.IO.MemoryStream outputTempFileStream = new System.IO.MemoryStream();

                    System.Drawing.Imaging.ImageFormat imageFormat;
                    System.Drawing.Bitmap bmp = Thinksea.Image.GetThumbnailImage(file, maxWidth, maxHeight, 保持原图比例, 保持小图尺寸, 图片质量, false, System.Drawing.Color.Transparent);
                    try
                    {
                        imageFormat = bmp.RawFormat;
                        if (转换为统一格式)
                        {
                            imageFormat = 中的统一格式.Format;
                            outputFile  = System.IO.Path.ChangeExtension(outputFile, 中的统一格式.Extension);
                        }
                        else
                        {
                            string ext = System.IO.Path.GetExtension(file);
                            foreach (var tmp in this.ImageFormats)
                            {
                                if (tmp.Extension == ext.ToLower())
                                {
                                    imageFormat = tmp.Format;
                                    break;
                                }
                            }
                        }

                        //bmp.Save(outputTempFile, imageFormat);
                        bmp.Save(outputTempFileStream, imageFormat);
                    }
                    finally
                    {
                        bmp.Dispose();
                    }

                    //                    {
                    //                        System.IO.FileInfo fiSource = new System.IO.FileInfo(file);
                    //                        System.IO.FileInfo fiTemp = new System.IO.FileInfo(outputTempFile);
                    //                        if (fiTemp.Length > fiSource.Length)
                    //                        {
                    //                            fiTemp.Delete();
                    //                            fiSource.CopyTo(outputTempFile);
                    //                            this.WriteLog(string.Format(@"文件压缩失败,由于压缩后比原始文件大,所以输出文件使用原始文件数据。
                    //    文件:{0}", file));
                    //                        }
                    //                    }

                    //                    if (System.IO.File.Exists(outputFile))
                    //                    {
                    //                        System.IO.File.Delete(outputFile);
                    //                    }
                    //                    System.IO.File.Move(outputTempFile, outputFile);
                    System.IO.FileInfo fiSource     = new System.IO.FileInfo(file);
                    System.IO.FileInfo fiOutputFile = new System.IO.FileInfo(outputFile);
                    if (outputTempFileStream.Length < fiSource.Length || fiSource.Extension.ToLower() != fiOutputFile.Extension.ToLower()) //如果可以压缩的更小或者输出文件类型不同,则保留压缩后的文件。
                    {
                        string outputTempFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(outputFile), System.Guid.NewGuid().ToString("N"));
                        System.IO.File.WriteAllBytes(outputTempFile, outputTempFileStream.ToArray()); //输出到临时文件。
                        if (System.IO.File.Exists(outputFile))                                        //删除目标文件。
                        {
                            System.IO.File.Delete(outputFile);
                        }
                        //if (fiSource.FullName != fiOutputFile.FullName) //删除原始文件。
                        //{
                        //    fiSource.Delete();
                        //}
                        System.IO.File.Move(outputTempFile, outputFile); //将临时文件名更名为目标文件。
                    }
                    else
                    {
                        if (fiSource.FullName.ToLower() != fiOutputFile.FullName.ToLower()) //如果原始文件与目标文件名称不同则直接改名,否则保留原始文件即可。
                        {
                            //fiTemp.Delete();
                            //fiSource.CopyTo(outputTempFile);
                            System.IO.File.Move(file, outputFile); //将临时文件名更名为目标文件。
                        }
                        this.WriteLog(string.Format(@"文件压缩失败,由于压缩后比原始文件大,所以输出文件使用原始文件数据。
    文件:{0}", file));
                    }
                }
                if (this.请求中止全部任务)
                {
                    //this.SetPropertyValue(this.toolStripStatusLabel正在处理的文件, "Text", "用户中止任务。");
                    this.正在处理的文件 = "用户中止任务。";
                }
                else
                {
                    this.SetPropertyValue(this.toolStripProgressBar1, "Value", files.Length);
                    //this.SetPropertyValue(this.toolStripStatusLabel正在处理的文件, "Text", "任务已完成。");
                    this.正在处理的文件 = "任务已完成。";
                }
            }
            finally
            {
                this.SetPropertyValue(this.toolStripButton添加文件, "Enabled", true);
                this.SetPropertyValue(this.toolStripButton添加文件夹, "Enabled", true);
                //this.SetPropertyValue(this.toolStripButton开始, "Enabled", true);
                this.SetPropertyValue(this.toolStripButton开始, "Image", global::CompressPics.Properties.Resources.run);
                this.SetPropertyValue(this.toolStripButton开始, "Text", "开始");
                this.SetPropertyValue(this.panel2, "Enabled", true);
                //this.timer1.Stop();
                this.thread压缩 = null;
            }
        }