C#获取图片文件扩展名的方法
作者:shichen2014 时间:2022-03-06 04:27:43
下面我给各位朋友整理了一篇C# 获取图片文件扩展名的例子,这里方法都非常的简单,我们只用到了image.RawFormat.Guid就实现了,具体看代码
例子
/// <summary>
/// 根据图像获取图像的扩展名
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static String GetExtension(Image image)
{
foreach (var pair in ImageFormats)
{
if (pair.Value.Guid == image.RawFormat.Guid)
{
return pair.Key;
}
}
throw new BadImageFormatException();
}
使用方法如下:
using (var img = Image.FromFile(@"C:soar"))
{
var ext = GetExtension(img);
}
补充方法:
public static bool CheckImgType(string strImg)
{
if(strImg!=null&&strImg.ToString().Length>0)
{
int i = strImg.LastIndexOf(".");
string StrType = strImg.Substring(i);
if (StrType == ".jpg" || StrType == ".gif" || StrType == ".jpeg" || StrType == ".png")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
C# 获取文件名及扩展名:
string aFirstName = aFile.Substring(aFile.LastIndexOf("\") + 1, (aFile.LastIndexOf(".") - aFile.LastIndexOf("\") - 1)); //文件名
string aLastName = aFile.Substring(aFile.LastIndexOf(".") + 1, (aFile.Length - aFile.LastIndexOf(".") - 1)); //扩展名
string strFilePaht="文件路径";
Path.GetFileNameWithoutExtension(strFilePath);这个就是获取文件名的
还有的就是用Substring截取
strFilePaht.Substring(path.LastIndexOf("\") + 1, path.Length - 1 - path.LastIndexOf("\"));
strFilePaht.Substring(path.LastIndexOf("."), path.Length - path.LastIndexOf("."));
或者用openFileDialog1.SafeFileName
这样就能取到该文件的所在目录路径
string path1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + @"";
string path = Path.GetFileName("C:My Documentpathimage.jpg"); //只获取文件名image.jpg
希望本文所述对大家的C#程序设计有所帮助。
标签:C#
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于java ssm springboot+mybatis酒庄内部管理系统设计和实现
2023-09-24 23:51:17
![](https://img.aspxhome.com/file/2023/9/58449_0s.jpg)
Java maven三种仓库,本地仓库,私服,中央仓库的配置
2023-04-13 12:05:17
![](https://img.aspxhome.com/file/2023/2/62132_0s.png)
Seata 环境搭建部署过程
2021-09-06 08:45:36
![](https://img.aspxhome.com/file/2023/8/78278_0s.png)
java实现二叉树遍历的三种方式
2021-12-03 06:11:51
![](https://img.aspxhome.com/file/2023/9/84179_0s.jpg)
基于servlet实现统计网页访问次数
2021-11-05 08:35:13
![](https://img.aspxhome.com/file/2023/7/82047_0s.jpg)
2021最新Android笔试题总结美团Android岗职能要求
2023-11-29 15:24:52
![](https://img.aspxhome.com/file/2023/9/93299_0s.jpg)
Spring Boot conditional注解用法详解
2022-03-19 02:32:25
![](https://img.aspxhome.com/file/2023/5/61795_0s.png)
Struts 2中的constant配置详解
2023-11-10 08:18:18
Java并发包线程池ThreadPoolExecutor的实现
2022-11-10 09:52:41
![](https://img.aspxhome.com/file/2023/3/64383_0s.jpg)
springBoot详解集成Swagger流程
2023-02-26 22:23:33
![](https://img.aspxhome.com/file/2023/0/89640_0s.png)
SpringBoot AOP AspectJ切面技术介绍与实现方式
2022-12-27 22:03:53
![](https://img.aspxhome.com/file/2023/0/84160_0s.png)
springboot实现通过路径从磁盘直接读取图片
2023-09-01 03:01:01
![](https://img.aspxhome.com/file/2023/9/80949_0s.png)
C#遍历文件夹后上传文件夹中所有文件错误案例分析
2022-11-03 09:28:27
Maven如何修改打包文件名称
2022-09-02 21:29:52
![](https://img.aspxhome.com/file/2023/5/67685_0s.png)
MyBatis利用MyCat实现多租户的简单思路分享
2022-08-16 18:58:33
![](https://img.aspxhome.com/file/2023/6/62716_0s.png)
详解path和classpath的区别
2023-05-02 13:28:32
java实现五子棋程序
2022-01-29 10:40:38
![](https://img.aspxhome.com/file/2023/7/63387_0s.jpg)
通过源码角度看看AccessibilityService
2023-07-25 09:31:43
![](https://img.aspxhome.com/file/2023/2/84752_0s.jpg)
图解二叉树的三种遍历方式及java实现代码
2022-02-08 12:03:32
![](https://img.aspxhome.com/file/2023/7/83027_0s.png)
浅谈Java安全之C3P0的使用
2022-06-06 00:22:11
![](https://img.aspxhome.com/file/2023/9/81349_0s.jpg)