Пример #1
0
        public static byte[] CompressImageByData(byte[] data, long maxLength, string sourceUrl)
        {
            System.Drawing.Image image = null;
            string value = string.Empty;

            if (!string.IsNullOrEmpty(sourceUrl) && sourceUrl.LastIndexOf(".") > 0)
            {
                value = sourceUrl.Substring(sourceUrl.LastIndexOf("."));
            }
            if (string.IsNullOrEmpty(value))
            {
                value = ".jpeg";
            }
            System.Drawing.Imaging.ImageFormat formatByExtension = ImageCompress.GetFormatByExtension(ref value);
            bool flag = false;

            do
            {
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream(data))
                    {
                        image = System.Drawing.Image.FromStream(memoryStream);
                        if (image == null)
                        {
                            break;
                        }
                        double towidth;
                        double toheight;
                        if (!flag)
                        {
                            towidth  = (double)image.Width;
                            toheight = (double)image.Height;
                        }
                        else
                        {
                            if (image.Width > 800)
                            {
                                towidth  = 800.0;
                                toheight = (double)image.Height * 800.0 / (double)image.Width;
                            }
                            else
                            {
                                towidth  = (double)image.Width * 0.9;
                                toheight = (double)image.Height * 0.9;
                            }
                        }
                        data = ImageCompress.MakeThumbnail(image, string.Empty, towidth, toheight, formatByExtension);
                        flag = true;
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteLog(ex);
                    image.Dispose();
                    break;
                }
                finally
                {
                    if (image != null)
                    {
                        image.Dispose();
                    }
                }
            }while ((long)data.Length >= maxLength);
            return(data);
        }
Пример #2
0
        public static byte[] CompressImage(string sourceFileFullName, long maxLength, out bool hasCompress)
        {
            hasCompress = false;
            byte[] array = ImageCompress.GetImageToByteArrayByFile(sourceFileFullName);
            if (array == null || (long)array.Length <= maxLength)
            {
                return(array);
            }
            System.Drawing.Image image = null;
            string text = string.Empty;

            if (!string.IsNullOrEmpty(sourceFileFullName) && sourceFileFullName.LastIndexOf(".") > 0)
            {
                text = sourceFileFullName.Substring(sourceFileFullName.LastIndexOf("."));
            }
            if (string.IsNullOrEmpty(text))
            {
                text = ".jpeg";
            }
            FileInfo fileInfo = new FileInfo(sourceFileFullName);
            string   text2    = fileInfo.DirectoryName;

            if (!text2.EndsWith("\\"))
            {
                text2 += "\\";
            }
            if (!Directory.Exists(text2))
            {
                Directory.CreateDirectory(text2);
            }
            System.Drawing.Imaging.ImageFormat formatByExtension = ImageCompress.GetFormatByExtension(ref text);
            string text3 = string.Concat(new object[]
            {
                text2,
                "Compress",
                DateTime.Now.Ticks,
                text
            });

            while (File.Exists(text3))
            {
                text3 = string.Concat(new object[]
                {
                    text2,
                    "Compress",
                    DateTime.Now.Ticks,
                    text
                });
            }
            bool flag = false;

            do
            {
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream(array))
                    {
                        image = System.Drawing.Image.FromStream(memoryStream);
                        if (image == null)
                        {
                            break;
                        }
                        double towidth;
                        double toheight;
                        if (!flag)
                        {
                            towidth  = (double)image.Width;
                            toheight = (double)image.Height;
                        }
                        else
                        {
                            if (image.Width > 800)
                            {
                                towidth  = 800.0;
                                toheight = (double)image.Height * 800.0 / (double)image.Width;
                            }
                            else
                            {
                                towidth  = (double)image.Width * 0.9;
                                toheight = (double)image.Height * 0.9;
                            }
                        }
                        array = ImageCompress.MakeThumbnail(image, text3, towidth, toheight, formatByExtension);
                        flag  = true;
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteLog(ex);
                    image.Dispose();
                    break;
                }
                finally
                {
                    if (image != null)
                    {
                        image.Dispose();
                    }
                }
            }while ((long)array.Length >= maxLength);
            hasCompress = true;
            return(array);
        }