C#实现根据图片的链接地址获取图片的后缀名
作者:£冷☆月№ 时间:2022-08-04 04:30:58
某天一朋友突然发来一个地址,问我怎么获取这张图片的后缀名??
将代码放在下面以供参考:
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);
}
}
}
来源:https://www.cnblogs.com/wsk198726/p/16670299.html
标签:C#,图片,后缀名
0
投稿
猜你喜欢
WPF中鼠标/键盘/拖拽事件以及用行为封装事件详解
2021-05-24 00:22:38
Eclipse打开时“发现了以元素'd:skin'”开头的无效内容。此处不应含有子元素的解决方法
2022-11-24 17:54:50
详解Spring MVC CORS 跨域
2023-11-25 08:04:37
Android Handle原理(Looper,Handler和Message)三者关系案例详解
2023-08-25 22:51:47
Android实现取消GridView中Item选中时默认的背景色
2023-02-26 05:53:30
C语言内存操作函数详解
2021-09-01 01:50:36
利用反射获取Java类中的静态变量名及变量值的简单实例
2022-12-09 17:56:06
Java生成含字母和数字的6位随机字符串
2023-04-02 02:28:58
java读取resources文件详解及实现代码
2022-07-15 15:11:30
Java掩码的几种使用例举
2022-02-11 11:08:04
Android 基于IntentService的文件下载的示例代码
2023-10-28 23:26:02
Java多线程锁机制相关原理实例解析
2023-05-25 12:33:37
Android仿iphone自定义滚动选择器
2023-06-24 12:47:59
分别在Groovy和Java中创建并初始化映射的不同分析
2023-11-26 08:15:42
Jenkins使用Gradle编译Android项目详解
2021-12-30 22:26:30
Jenkins Pipeline 部署 SpringBoot 应用的教程详解
2022-09-26 14:06:27
Java实现断点下载服务端与客户端的示例代码
2022-07-25 14:16:17
c#裁剪图片后使用zxing生成二维码示例分享
2021-09-08 17:06:58
android studio 3.0 升级 项目遇到的问题及更改思路(问题小结)
2021-06-14 17:19:16
Java 线程的优先级(setPriority)案例详解
2023-11-12 23:46:39