Setting Windows 8 account picture with .NET/WinRT hybrid
Basically I'm trying to set a Windows 8 account picture from a "Console Application". The only API to set the picture that I found is in the WinRT API. So after blood, sweat, and tears I finally managed to get a hybrid .NET/WinRT somewhat working, but the clue being that it doesn't actually work.
Anyway, this is the code so far:
Code:
private static async Task SetAccountPicture(string employeeId)
{
// Download image
string imagePath = DownloadImage(employeeId);
// Set account picture
Stream fileStream = File.Open(imagePath, FileMode.Open);
IRandomAccessStream winRTStream = await DotNetToWinRTStream(fileStream);
SetAccountPictureResult result = await UserInformation.SetAccountPicturesFromStreamsAsync(null, winRTStream, null);
// Clean up download file
File.Delete(imagePath);
}
public static async Task<IRandomAccessStream> DotNetToWinRTStream(Stream dotNetStream)
{
IBuffer buffer;
var inputStream = dotNetStream.AsInputStream();
using (var reader = new DataReader(inputStream))
{
await reader.LoadAsync((uint)dotNetStream.Length);
buffer = reader.DetachBuffer();
}
var memoryStream = new InMemoryRandomAccessStream();
await memoryStream.WriteAsync(buffer);
return memoryStream;
}
The SetAccountPictureResult says 'failure' without any other information. What could be happening here?
Re: Setting Windows 8 account picture with .NET/WinRT hybrid
Not sure if you have seen this but I cannot check it anyway since I don't have Windows 8.
Re: Setting Windows 8 account picture with .NET/WinRT hybrid
Yes, I have seen that. That's actually where I got the WinRT code from, but when used in a regular .NET application it doesn't seem to work.
1 Attachment(s)
Re: Setting Windows 8 account picture with .NET/WinRT hybrid
I'm having the exact same problem. Everything about using "Windows Runtime" methods from regular .NET apps has been incredibly frustrating and troublesome so far. I'm doing it slightly differently to you but still getting the same problem (i.e. no exception thrown but the returned value is just "Failure"). I've also verified that the Windows.System.UserProfile.UserInformation.AccountPictureChangeEnabled property is True - it was initially False but then I went into control panel or whatever the hell it is called on Windows 8 now and set the "Let apps use my name and account picture" option to On (stupid how that setting implies apps will only be able to view it not change it, but the AccountPictureChangedEnabled property implies apps are now allowed to change it as well).
Here's how I'm doing it, in case anyone can spot any problems:
Attachment 105353
Did you ever find a solution?