常用的图片处理

来源:互联网 发布:淘宝如何设置免运费 编辑:程序博客网 时间:2024/05/16 17:56
 internal static class FileHelper    {        public static readonly Dictionary<string, string> fileFixDic = new Dictionary<string, string>           {               {"255216",".jpg"},               {"708783",".swf"},               {"678783",".swf"},               {"707686",".flv"},             };        /// <summary>        /// 下载图片        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static Image DownLoadImage(string url)        {            System.Exception exception;            return DownLoadImage(url, out exception);        }        /// <summary>        /// 下载图片        /// </summary>        /// <param name="url">图片地址</param>        /// <param name="exception"></param>        /// <param name="isRetry">是否重试</param>        /// <param name="reTryNumber">重试次数</param>        /// <returns>Image</returns>        public static Image DownLoadImage(string url, out System.Exception exception, bool isRetry = true, int reTryNumber = 2)        {            byte[] data = DownLoadWebFile(url, out exception, isRetry, reTryNumber);            return GetImageByByte(data);        }        /// <summary>        /// 下载资源文件        /// </summary>        /// <param name="url">资源文件地址</param>        /// <returns>byte[]</returns>        public static byte[] DownLoadWebFile(string url)        {            System.Exception exception;            return DownLoadWebFile(url, out exception);        }        /// <summary>        /// 下载资源文件        /// </summary>        /// <param name="url">资源文件地址</param>        /// <param name="exception">Exception</param>        /// <param name="isRetry">是否重试,默认是</param>        /// <param name="reTryNumber">重试次数 默认2</param>        /// <returns>byte[]</returns>        public static byte[] DownLoadWebFile(string url, out System.Exception exception, bool isRetry = true, int reTryNumber = 2)        {            var client = new WebClient();                        byte[] data = null;            exception = null;            while (reTryNumber > 0)            {                try                {                    data = client.DownloadData(url);                    reTryNumber = 0;                }                catch (System.Exception ex)                {                    reTryNumber--;                    if (reTryNumber == 0)                    {                        exception = ex;                    }                }            }            return data;        }        /// <summary>        /// 检查上传图片文件的像素是否符合        /// </summary>        /// <param name="image"></param>        /// <returns></returns>        public static bool CheckImagePixels(Image image)        {            //标准图片:1280X1024、800X600、640X480            if (image.Width == 1280 && image.Height == 1024)            {                return true;            }            if (image.Width == 800 && image.Height == 600)            {                return true;            }            if (image.Width == 640 && image.Height == 480)            {                return true;            }            return false;        }        /// <summary>        /// Byet Convert Image        /// </summary>        /// <param name="bytes"></param>        /// <returns></returns>        public static Image GetImageByByte(byte[] bytes)        {            if (null == bytes)            {                return null;            }            var ms = new MemoryStream(bytes);            return Image.FromStream(ms);        }        /// <summary>        /// 生成缩略图        /// </summary>        /// <param name="image">原图</param>        /// <param name="width">宽</param>        /// <param name="height">高</param>        /// <returns>新图</returns>        public static Bitmap FixedSize(Image image, int width, int height)        {            int sourceWidth = image.Width;            int sourceHeight = image.Height;            int sourceX = 0;            int sourceY = 0;            int destX = 0;            int destY = 0;            float nPercent = 0;            float nPercentW = 0;            float nPercentH = 0;            nPercentW = Math.Min((float)width / (float)sourceWidth, 1.0F);            nPercentH = Math.Min(((float)height / (float)sourceHeight), 1.0F);            //if we have to pad the height pad both the top and the bottom            //with the difference between the scaled height and the desired height            nPercent = Math.Min(nPercentH, nPercentW);            destX = (int)((width - (sourceWidth * nPercent)) / 2);            destY = (int)((height - (sourceHeight * nPercent)) / 2);            int destWidth = (int)(sourceWidth * nPercent);            int destHeight = (int)(sourceHeight * nPercent);            Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format24bppRgb);            bmPhoto.SetResolution(image.HorizontalResolution, image.VerticalResolution);            Graphics grPhoto = Graphics.FromImage(bmPhoto);            grPhoto.Clear(Color.White);            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;            grPhoto.DrawImage(image,                new Rectangle(destX, destY, destWidth, destHeight),                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),                GraphicsUnit.Pixel);            grPhoto.Dispose();            return bmPhoto;        }        /// <summary>        /// 获取文件的类型        /// </summary>        /// <param name="bytes"></param>        /// <returns></returns>        public static string GetFileFix(byte[] bytes)        {            if (bytes.Length < 3)            {                return string.Empty;            }            string fileType = string.Format("{0}{1}{2}", bytes[0], bytes[1], bytes[2]);            return GetFileFix(fileType);        }        /// <summary>        /// 获取文件的类型        /// </summary>        /// <param name="fileStream"></param>        /// <returns></returns>        public static string GetFileFix(FileStream fileStream)        {            if (fileStream.Length < 3)            {                return string.Empty;            }            return GetFileFix(SteamToBytes(fileStream));        }        private static string GetFileFix(string fileType)        {            foreach (var key in fileFixDic.Keys)            {                if (fileType.Contains(key))                {                    return fileFixDic[key];                }            }            return string.Empty;        }        /// <summary>        /// 将流文件转换为Byte数据        /// </summary>        /// <param name="stream"></param>        /// <returns></returns>        private static byte[] SteamToBytes(Stream stream)        {            byte[] bytes = new byte[stream.Length];            stream.Read(bytes, 0, bytes.Length);            stream.Seek(0, SeekOrigin.Begin);            return bytes;        }        public static void CopyStream(Stream sourceStream, Stream destStream)        {            if (sourceStream == null)            {                throw new System.Exception("Source Stream can't be empty.");            }            if (destStream == null)            {                throw new System.Exception("destination Stream can't be empty.");            }            byte[] buffer = new byte[1024 * 8];            int count = 0;            while ((count = sourceStream.Read(buffer, 0, buffer.Length)) > 0)            {                destStream.Write(buffer, 0, count);            }        }    }