nie kruk
nie kruk

Reputation: 1

How to get steam avatar with steamworks in c#?

I'm programming in c# .NET I'm not using Unity and I want by means of steamworks collect and put player's avatar in picturebox. I did SOMETHING, but don't know what's next. I'm new in steamworks and I can't using it correctly.

var avatarInt = SteamFriends.GetMediumFriendAvatar(SteamUser.GetSteamID());
uint Width, Height;
SteamUtils.GetImageSize(avatarInt, out Width, out Height);
var abc_test = (SteamUser.GetSteamID().ToString());
byte[] avatarstream = new byte[4 * (int)Width * (int)Height];
SteamUtils.GetImageRGBA(avatarInt, avatarstream, 4 * (int)Width * (int)Height);

Earlier I did something similar with player's nick.

nick_steam.Text = SteamFriends.GetPersonaName(); 

Upvotes: 0

Views: 6275

Answers (2)

Dimolade
Dimolade

Reputation: 1

From your already existing code, I made this. To use it on a PictureBox you can just do:

pictureBox.Image = avatarBitmap;

var avatarInt = SteamFriends.GetMediumFriendAvatar(SteamUser.GetSteamID());
uint Width, Height;
SteamUtils.GetImageSize(avatarInt, out Width, out Height);

byte[] avatarStream = new byte[4 * (int)Width * (int)Height];
SteamUtils.GetImageRGBA(avatarInt, avatarStream, 4 * (int)Width * (int)Height);

// Create a Bitmap with the correct dimensions
Bitmap avatarBitmap = new Bitmap((int)Width, (int)Height);

// Lock the Bitmap data
BitmapData bmpData = avatarBitmap.LockBits(new Rectangle(0, 0, (int)Width, (int)Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

// Create a color array to store the corrected colors
byte[] colors = new byte[4];

// Copy and correct the image data
for (int y = 0; y < Height; y++)
{
    for (int x = 0; x < Width; x++)
    {
        int index = (y * (int)Width + x) * 4;

        colors[0] = avatarStream[index + 2]; // Red channel
        colors[1] = avatarStream[index + 1]; // Green channel
        colors[2] = avatarStream[index + 0]; // Blue channel
        colors[3] = avatarStream[index + 3]; // Alpha channel

        Marshal.Copy(colors, 0, (IntPtr)(bmpData.Scan0.ToInt64() + index), 4);
    }
}

// Unlock the Bitmap data
avatarBitmap.UnlockBits(bmpData);

Upvotes: 0

Cleptus
Cleptus

Reputation: 3541

From the Steamworks github:

https://github.com/rlabrecque/Steamworks.NET-Test/blob/master/Assets/Scripts/SteamUtilsTest.cs

public static Texture2D GetSteamImageAsTexture2D(int iImage) {
    Texture2D ret = null;
    uint ImageWidth;
    uint ImageHeight;
    bool bIsValid = SteamUtils.GetImageSize(iImage, out ImageWidth, out ImageHeight);

    if (bIsValid) {
        byte[] Image = new byte[ImageWidth * ImageHeight * 4];

        bIsValid = SteamUtils.GetImageRGBA(iImage, Image, (int)(ImageWidth * ImageHeight * 4));
        if (bIsValid) {
            ret = new Texture2D((int)ImageWidth, (int)ImageHeight, TextureFormat.RGBA32, false, true);
            ret.LoadRawTextureData(Image);
            ret.Apply();
        }
    }

    return ret;
}

Upvotes: 1

Related Questions