C#获取图片文件扩展名的方法
下面我给各位朋友整理了一篇C#获取图片文件扩展名的例子,这里方法都非常的简单,我们只用到了image.RawFormat.Guid就实现了,具体看代码
例子
///根据图像获取图像的扩展名
///</summary>
///<paramname="image"></param>
///<returns></returns>
publicstaticStringGetExtension(Imageimage)
{
foreach(varpairinImageFormats)
{
if(pair.Value.Guid==image.RawFormat.Guid)
{
returnpair.Key;
}
}
thrownewBadImageFormatException();
}
使用方法如下:
{
varext=GetExtension(img);
}
补充方法:
C#获取文件名及扩展名: 希望本文所述对大家的C#程序设计有所帮助。
{
if(strImg!=null&&strImg.ToString().Length>0)
{
inti=strImg.LastIndexOf(".");
stringStrType=strImg.Substring(i);
if(StrType==".jpg"||StrType==".gif"||StrType==".jpeg"||StrType==".png")
{
returntrue;
}
else
{
returnfalse;
}
}
else
{
returnfalse;
}
}
stringaLastName=aFile.Substring(aFile.LastIndexOf(".")+1,(aFile.Length-aFile.LastIndexOf(".")-1)); //扩展名
stringstrFilePaht="文件路径";
Path.GetFileNameWithoutExtension(strFilePath);这个就是获取文件名的
还有的就是用Substring截取
strFilePaht.Substring(path.LastIndexOf("."),path.Length-path.LastIndexOf("."));
或者用openFileDialog1.SafeFileName
这样就能取到该文件的所在目录路径
stringpath=Path.GetFileName("C:MyDocumentpathimage.jpg"); //只获取文件名image.jpg
相关文章