C# - How to get current user picture

落花浮王杯 提交于 2020-01-05 07:37:56

问题


how can I get the image of the logged in user in c#? Thanks


回答1:


That is not possible on each OS; but on the OS'es where it is possible:

For Windows Vista or later:

That image is located in C:\Users\UserName\AppData\Local\Temp\UserName.bmp

if the user is a domain user, it will be "DOMAIN+UserName.bmp" (Yes, the '+' is part of the filename)




回答2:


Try Ths::

    public static Image GetUserimage()
    {  

        if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+@"\Temp\"+Environment.UserName+".bmp"))
        {
            return Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Temp\" + Environment.UserName + ".bmp");
        }
        else
        {
            return null;
        }
    }



回答3:


Looks like Joco and this guy have dug into some tools and AD aspects of setting the "usertile"




回答4:


Try this for windows 10

The user picture is located at C:/users/username/appdata/romaing/microsoft/windows/account pictures/




回答5:


For current user, and ANY user on a Microsoft Windows instance, there is a NuGet package for this (and here goes the shameless plug):

https://github.com/HTD/Woof.System

For those interested in source code and voodoo magic involved.

For those interested in just making it work, use NuGet package Woof.System, then you use the feature like this:

Get the small current user bitmap for WPF:
var userBitmapSmall = new BitmapImage(new Uri(SysInfo.GetUserPicturePath()));
Get the small OTHER user bitmap for WPF:
var userBitmapSmall = new BitmapImage(new Uri(SysInfo.GetUserPicturePath("John")));
Get the small and LARGE ANY user bitmap for WPF:
var smallBitmapPath = SysInfo.GetUserPicturePath("John", out var largeBitmapPath);
var smallBitmap = new BitmapImage(new Uri(smallBitmapPath));
var largeBitmap = new BitmapImage(new Uri(largeBitmapPath));

The solution contained in the library is based on undocumented Shell32.dll calls present in Windows Vista and newer and undocumented registry keys present in Windows 8 and newer.

How to get the profile picture of a user logged with Microsoft Account is my own discovery, I haven't found the solution anywhere, it's probably Microsoft's sweet secret.

What's important, solution that can be googled are incomplete in many ways:

  • without Win32 API the user profile picture could be either not present in temporary directory, can be deleted, can be inaccessible for a regular user to read.
  • without using undocumented registry key and undocumented Win32 API features retrieving profile picture for Microsoft Account user is impossible.

If you find any bugs or incompatibility in this solution please open an issue on GitHub.

Tested with a couple of versions of Windows 10 (stable and developers build). I tested it with local and Microsoft Accounts.

BTW: If the user name doesn't exist, the method returns a path to "default" profile picture.

NOTE: The package depends on .NET Framework and is incompatible with .NET Core and .NET Standard due to the strong dependency on Win32 API (and thus not cross platform). There are other Woof libraries compatible with .NET Standard. They seem undocumented, however they contain XML documentation available for Visual Studio and the source code on GitHub allows figuring out how they work. Woof libraries are in constant active development, so it's a safe choice.



来源:https://stackoverflow.com/questions/5570113/c-sharp-how-to-get-current-user-picture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!