作为练习,我正在尝试移植 Overv/SteamWebAPI到可移植类库。但是,其中一个函数返回 System.Drawing.Bitmap
,这在 .NET Portable 子集中不可用。
考虑到下面的功能,最好的选择是什么?由于项目的性质,我不关心向后兼容性。
有问题的功能:
/// <summary>
/// Retrieve the avatar of the specified user in the specified format.
/// </summary>
/// <param name="user">User</param>
/// <param name="size">Requested avatar size</param>
/// <returns>The avatar as bitmap on success or null on failure.</returns>
public Bitmap GetUserAvatar(User user, AvatarSize size = AvatarSize.Small)
{
if (user.avatarUrl.Length == 0) return null;
try
{
WebClient client = new WebClient();
Stream stream;
if (size == AvatarSize.Small)
stream = client.OpenRead(user.avatarUrl + ".jpg");
else if (size == AvatarSize.Medium)
stream = client.OpenRead(user.avatarUrl + "_medium.jpg");
else
stream = client.OpenRead(user.avatarUrl + "_full.jpg");
Bitmap avatar = new Bitmap(stream);
stream.Flush();
stream.Close();
return avatar;
}
catch (Exception e)
{
return null;
}
}
最佳答案
可移植类库确实 not contain图形或 Web 客户端支持。
如果这是唯一有问题的方法,也许你可以在你的可移植库中丢弃这个唯一的方法。或者,您可以考虑返回 Stream,但您仍然需要找到从 Web 读取位图的解决方法。
更新
据我所知,该方法可以静态化。因此,另一种替代方法是为不可移植代码创建额外的平台特定(.NET、Silverlight、WP7)库,并将此方法移至平台特定库中的静态类。如果您使该方法成为扩展方法,您将能够在不修改客户端代码的情况下使用该方法。当然,除了该方法应该返回 System.Windows.Media.Imaging.BitmapSource。而不是 System.Drawing.Bitmap。
关于c# - .NET 可移植框架中的 System.Drawing.Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583674/