Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Get user picture (Avatar)

I'm referring to C# - How to get current user picture but I never found a solution. (I'm working on Win7 OS)

For some users, the picture is located on

C:\Users\UserName\AppData\Local\Temp\UserName.bmp

(where UserName is user nickname) for others users this path throws FileNotFoundException but pictures exists.

Where I can find information about the path or real picture? There is a registry that contains this information?

like image 689
CeccoCQ Avatar asked Aug 31 '11 07:08

CeccoCQ


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

This blog post shows how to set the user tile (picture). In a comment near the end (Michael Anthony, Apr 10, 22:45), the commenter describes how to get the picture. I've gathered the info into a C# snippet. Remember that this is based on an undocumented Windows Shell function.

    using System;
    using System.Text;
    using System.Drawing;

    [DllImport("shell32.dll", EntryPoint = "#261", 
               CharSet = CharSet.Unicode, PreserveSig = false)]
    public static extern void GetUserTilePath(
      string username, 
      UInt32 whatever, // 0x80000000
      StringBuilder picpath, int maxLength);

    public static string GetUserTilePath(string username)
    {   // username: use null for current user
        var sb = new StringBuilder(1000);
        GetUserTilePath(username, 0x80000000, sb, sb.Capacity);
        return sb.ToString();
    }

    public static Image GetUserTile(string username)
    {
        return Image.FromFile(GetUserTilePath(username));
    }

Note that this Shell function creates the file \Users\<USER>\AppData...\<USER>.bmp and returns its filename.

Also, I've tested it on Win7. I have no idea of its compatibility with former Windows versions.

Credits to Joco and Michael Anthony.

like image 151
Serge Wautier Avatar answered Sep 30 '22 01:09

Serge Wautier