C#實(shí)現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名
某天一朋友突然發(fā)來一個(gè)地址,問我怎么獲取這張圖片的后綴名??
將代碼放在下面以供參考:
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路徑圖片轉(zhuǎn)為byte字節(jié)數(shù)據(jù) /// </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字節(jié)數(shù)據(jù)轉(zhuǎn)為Image圖片 /// </summary> /// <param name="bytes">字節(jié)數(shù)組</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圖片轉(zhuǎn)為byte字節(jié)數(shù)據(jù) /// </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);//將圖像以指定的格式存入緩存內(nèi)存流 bt = new byte[mostream.Length]; mostream.Position = 0;//設(shè)置留的初始位置 mostream.Read(bt, 0, Convert.ToInt32(bt.Length)); } } return bt; } /// <summary> /// 根據(jù)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); } } }
到此這篇關(guān)于C#實(shí)現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名的文章就介紹到這了,更多相關(guān)C#獲取圖片后綴名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在WinForm中發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄赪inForm中發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06Unity3D UI Text得分?jǐn)?shù)字增加的實(shí)例代碼
這篇文章主要介紹了Unity3D UI Text得分?jǐn)?shù)字增加方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#獲取變更過的DataTable記錄的實(shí)現(xiàn)方法
這篇文章主要介紹了C#獲取變更過的DataTable記錄的實(shí)現(xiàn)方法,對(duì)初學(xué)者很有學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法
這篇文章主要介紹了C#實(shí)現(xiàn)winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法,涉及C#窗體交互的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07