2012-07-20 52 views
3

作为练习,我试图将Overv/SteamWebAPI移植到可移植类库中。但是,其中一个函数返回一个System.Drawing.Bitmap,这在.NET Portable子集中不可用。.NET Portable Framework中的System.Drawing.Bitmap

考虑到下面的功能,最好的选择是什么?由于该项目的性质,我不关心后向兼容性问题。

有问题的功能:

/// <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; 
    } 
} 

回答

3

可移植类库确实not contain显卡也不Web客户端的支持。

如果这是唯一违规的方法,那么也许你可以在你的便携库中丢弃这个唯一的方法。或者,您可以考虑返回,尽管您仍然需要找到一种解决方法来从Web中读取位图。

UPDATE

据我所知,该方法可以进行静态的。因此,另一种选择是为非可移植代码创建其他平台特定的(.NET,Silverlight,WP7)库,并将此方法移至特定于平台的库中的静态类。如果您使该方法成为扩展方法,那么您将能够使用该方法,而无需修改客户端代码。当然,除了该方法应返回System.Windows.Media.Imaging.BitmapSource而不是System.Drawing.Bitmap

+0

但要小心:'System.Windows.Media.Imaging.BitmapSource'在线程中播放效果并不好。 – vines 2017-09-06 11:34:00