常用的图片处理

 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);
            }
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值