C#如何获取用户头像

用户头像的路径是 %appdata%\Microsoft\Windows\AccountPictures

 直接通过下面的代码可以获取所有头像文件

1  var path = Environment.ExpandEnvironmentVariables("%appdata%\\Microsoft\\Windows\\AccountPictures");
2  var files = System.IO.Directory.GetFiles(path);

 

如果有多个文件,取最近修改的那个文件即可。

不过获取出来的文件是.accountpicture-ms格式,我们通过下面的方式提取即可

 

小图

 1  public static Bitmap GetImage96(string path)
 2  {
 3      FileStream fs = new FileStream(path, FileMode.Open);
 4      long position = Seek(fs, "JFIF", 0);
 5      byte[] b = new byte[Convert.ToInt32(fs.Length)];
 6      fs.Seek(position - 6, SeekOrigin.Begin);
 7      fs.Read(b, 0, b.Length);
 8      fs.Close();
 9      fs.Dispose();
10      return GetBitmapImage(b);
11  }

 

大图

 1   public static Bitmap GetImage448(string path)
 2   {
 3       FileStream fs = new FileStream(path, FileMode.Open);
 4       long position = Seek(fs, "JFIF", 100);
 5       byte[] b = new byte[Convert.ToInt32(fs.Length)];
 6       fs.Seek(position - 6, SeekOrigin.Begin);
 7       fs.Read(b, 0, b.Length);
 8       fs.Close();
 9       fs.Dispose();
10       return GetBitmapImage(b);
11   }

 

提取的方式来自:https://github.com/Efreeto/AccountPicConverter/blob/master/AccountPicConverter.cs

posted @ 2024-06-04 16:58  zhaotianff  阅读(24)  评论(0编辑  收藏  举报