亲宝软件园·资讯

展开

C#实现根据图片的链接地址获取图片的后缀名

£冷☆月№ 人气:0

某天一朋友突然发来一个地址,问我怎么获取这张图片的后缀名??

将代码放在下面以供参考:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var imgUrl = "http://emoji.qpic.cn/wx_emoji/haiannhLHhY7B1tX6eZ2BGNh9kzx3VCQ2MJfSQkSgE47sEXofVVoPCiaZKYbPcyQhS/";
            var imgByte = GetBytesFromUrl(imgUrl);
            if (imgByte.Length > 0)
            {
                var image = BytesToImage(imgByte);
                if (image != null)
                {
                    var mimeType = GetMimeType(image);
                    if (!string.IsNullOrEmpty(mimeType))
                    {
                        var fileName = AppDomain.CurrentDomain.BaseDirectory + "\\imgses\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + mimeType;
                        CreateImageFromBytes(fileName, imgByte);
                    }
                }
            }
        }

        /// <summary>
        /// 将http路径图片转为byte字节数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static byte[] GetBytesFromUrl(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            byte[] bytes;
            using (Stream stream = request.GetResponse().GetResponseStream())
            {
                using (MemoryStream mstream = new MemoryStream())
                {
                    int count = 0;
                    byte[] buffer = new byte[1024];
                    int readNum = 0;
                    while ((readNum = stream.Read(buffer, 0, 1024)) > 0)
                    {
                        count = count + readNum;
                        mstream.Write(buffer, 0, readNum);
                    }
                    mstream.Position = 0;
                    using (BinaryReader br = new BinaryReader(mstream))
                    {
                        bytes = br.ReadBytes(count);
                    }
                }
            }
            return bytes;
        }

        /// <summary>
        /// 将byte字节数据转为Image图片
        /// </summary>
        /// <param name="bytes">字节数组</param>
        /// <returns>图片</returns>
        public static Image BytesToImage(byte[] bytes)
        {
            Image image = null;
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                ms.Write(bytes, 0, bytes.Length);
                image = Image.FromStream(ms, true);
            }
            return image;
        }
        
        /// <summary>
        /// 将Image图片转为byte字节数据
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToBytes(Image image)
        {
            byte[] bt = null;
            if (!image.Equals(null))
            {
                using (MemoryStream mostream = new MemoryStream())
                {
                    Bitmap bmp = new Bitmap(image);
                    bmp.Save(mostream, ImageFormat.Bmp);//将图像以指定的格式存入缓存内存流
                    bt = new byte[mostream.Length];
                    mostream.Position = 0;//设置留的初始位置
                    mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
                }
            }
            return bt;
        }

        /// <summary>
        /// 根据Image图片获取图片类型
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static string GetMimeType(Image image)
        {
            var mimeType = "";
            var ImageCodec = ImageCodecInfo.GetImageDecoders();
            foreach (var item in ImageCodec)
            {
                if (item.FormatID == image.RawFormat.Guid)
                {
                    mimeType = item.MimeType.Split('/')[1];
                    break;
                }
            }
            return mimeType;
        }

        /// <summary>
        /// 将byte[]图片保存到指定文件
        /// </summary>
        /// <param name="fileName">保存的完整路径(包含文件名)</param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static void CreateImageFromBytes(string fileName, byte[] bytes)
        {
            string file = fileName;
            FileInfo info = new FileInfo(fileName);
            Directory.CreateDirectory(info.Directory.FullName);
            File.WriteAllBytes(file, bytes);
        }
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论