Created
May 23, 2011 15:17
-
-
Save kimoto/986866 to your computer and use it in GitHub Desktop.
Get Friend's Avator Picture (Steam) - OpenSteamWorks with C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using System.Windows.Forms; | |
| using System.Drawing; | |
| using System.Drawing.Imaging; | |
| using System.Runtime.InteropServices; | |
| using Steam4NET; | |
| namespace FriendManager | |
| { | |
| class Program | |
| { | |
| // 指定されたIDのフレンドのAvatorのビットマップを取得します | |
| public static Bitmap getFriendAvatorBitmap(ulong friendId) | |
| { | |
| Friend friend = new Friend(friendId); | |
| int avatorHandle = SteamContext.ClientFriends.GetLargeFriendAvatar(friendId); | |
| uint width = 0; | |
| uint height = 0; | |
| if (SteamContext.SteamUtils.GetImageSize(avatorHandle, ref width, ref height)) | |
| { | |
| uint size = 4 * width * height; // magic number(4) は、rgbaの4byteの意味 | |
| byte[] buf = new byte[size]; | |
| if (SteamContext.SteamUtils.GetImageRGBA(avatorHandle, buf, (int)size)) | |
| { | |
| Bitmap b = new Bitmap((int)width, (int)height); | |
| for (uint xx = 0; xx < width; xx++) | |
| { | |
| for (uint yy = 0; yy < height; yy++) | |
| { | |
| int index = ((int)yy * (int)width + (int)xx) * 4; | |
| int rr = buf[index + 0]; | |
| int gg = buf[index + 1]; | |
| int bb = buf[index + 2]; | |
| int aa = buf[index + 3]; | |
| Color color = Color.FromArgb(aa, rr, gg, bb); | |
| b.SetPixel((int)xx, (int)yy, color); | |
| } | |
| } | |
| return b; | |
| } | |
| } | |
| return null; | |
| } | |
| // ファイル名から危険な文字を取り除きます | |
| public static String trimIllegalCode(String input) | |
| { | |
| foreach (char c in System.IO.Path.GetInvalidFileNameChars()) | |
| { | |
| input = input.Replace(c, '_'); | |
| } | |
| return input; | |
| } | |
| // 指定されたIDのフレンドのAvatorを保存します | |
| public static void saveFriendAvator(String basePath, ulong friendId) | |
| { | |
| Friend friend = new Friend(friendId); | |
| Bitmap b = getFriendAvatorBitmap(friendId); | |
| if (b != null) | |
| { | |
| String filename = "avator-" + trimIllegalCode(friend.GetName()) + ".png"; | |
| b.Save(basePath + filename); | |
| } | |
| } | |
| // すべてのフレンドのavatorを保存します | |
| public static int saveAllFriendAvators(String basePath) | |
| { | |
| int cnt = 0; | |
| int friendCount = SteamContext.ClientFriends.GetFriendCount((int)EFriendFlags.k_EFriendFlagImmediate); | |
| for (int x = 0; x < friendCount; ++x) | |
| { | |
| ulong friendId = SteamContext.ClientFriends.GetFriendByIndex(x, (int)EFriendFlags.k_EFriendFlagImmediate); | |
| saveFriendAvator(basePath, friendId); | |
| cnt++; | |
| } | |
| return cnt; | |
| } | |
| public static void Main() | |
| { | |
| try | |
| { | |
| SteamContext.Initialize(); | |
| MessageBox.Show(saveAllFriendAvators(@"png\avator-") + "件画像ファイルを保存しました"); | |
| } | |
| catch ( Exception ex ) | |
| { | |
| Util.MsgBox( "Unable to initialize Steam Context: " + ex.Message + "\n\n" + ex.ToString() ); | |
| return; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment