One day, a friend suddenly sent me an address and asked me how to get the suffix name of this picture? ?
Put the code below for reference:
using System; using ; using ; using ; using ; namespace ConsoleApp3 { class Program { static void Main(string[] args) { var imgUrl = "/wx_emoji/haiannhLHhY7B1tX6eZ2BGNh9kzx3VCQ2MJfSQkSgE47sEXofVVoPCiaZKYbPcyQhS/"; var imgByte = GetBytesFromUrl(imgUrl); if ( > 0) { var image = BytesToImage(imgByte); if (image != null) { var mimeType = GetMimeType(image); if (!(mimeType)) { var fileName = + "\\imgses\\" + ("yyyyMMddHHmmssfff") + "." + mimeType; CreateImageFromBytes(fileName, imgByte); } } } } /// <summary> /// Convert http path image to byte data /// </summary> /// <param name="url"></param> /// <returns></returns> public static byte[] GetBytesFromUrl(string url) { HttpWebRequest request = (HttpWebRequest)(url); byte[] bytes; using (Stream stream = ().GetResponseStream()) { using (MemoryStream mstream = new MemoryStream()) { int count = 0; byte[] buffer = new byte[1024]; int readNum = 0; while ((readNum = (buffer, 0, 1024)) > 0) { count = count + readNum; (buffer, 0, readNum); } = 0; using (BinaryReader br = new BinaryReader(mstream)) { bytes = (count); } } } return bytes; } /// <summary> /// Convert byte byte data to Image image /// </summary> /// <param name="bytes">Byte array</param> /// <returns>Picture</returns> public static Image BytesToImage(byte[] bytes) { Image image = null; using (MemoryStream ms = new MemoryStream(bytes)) { (bytes, 0, ); image = (ms, true); } return image; } /// <summary> /// Convert Image image to byte data /// </summary> /// <param name="image"></param> /// <returns></returns> public static byte[] ImageToBytes(Image image) { byte[] bt = null; if (!(null)) { using (MemoryStream mostream = new MemoryStream()) { Bitmap bmp = new Bitmap(image); (mostream, );//Storing the image into the cache memory stream in the specified format bt = new byte[]; = 0;//Set the initial position to be left (bt, 0, Convert.ToInt32()); } } return bt; } /// <summary> /// Get the image type according to the Image image /// </summary> /// <param name="image"></param> /// <returns></returns> public static string GetMimeType(Image image) { var mimeType = ""; var ImageCodec = (); foreach (var item in ImageCodec) { if ( == ) { mimeType = ('/')[1]; break; } } return mimeType; } /// <summary> /// Save the byte[] image to the specified file /// </summary> /// <param name="fileName">Full path saved (including file name)</param> /// <param name="bytes"></param> /// <returns></returns> public static void CreateImageFromBytes(string fileName, byte[] bytes) { string file = fileName; FileInfo info = new FileInfo(fileName); (); (file, bytes); } } }
This is the article about C#’s implementation of obtaining the suffix name of the image based on the link address of the image. For more related content of C# to obtain the suffix name of the image, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!