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   }

提取的方式来自:AccountPicConverter/AccountPicConverter.cs at master · Efreeto/AccountPicConverter · GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值