【问题标题】:System.Drawing.Bitmap in the .NET Portable Framework.NET 可移植框架中的 System.Drawing.Bitmap
【发布时间】:2012-07-20 16:46:17
【问题描述】:

作为练习,我正在尝试将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;
    }
}

【问题讨论】:

    标签: c# porting portable-class-library


    【解决方案1】:

    可移植类库不支持not contain 图形也不支持网络客户端。

    如果这是唯一有问题的方法,也许你可以在你的可移植库中丢弃这个唯一的方法。或者,您可以考虑返回 Stream,尽管您仍然需要找到一种解决方法来从 Web 读取位图。

    更新

    据我所知,该方法可以设为静态。因此,另一种选择是为不可移植代码创建额外的平台特定(.NET、Silverlight、WP7)库,并将此方法移动到平台特定库中的静态类。如果将该方法设为扩展方法,则无需修改客户端代码即可使用该方法。当然,除了该方法应该返回 System.Windows.Media.Imaging.BitmapSource 而不是 System.Drawing.Bitmap

    【讨论】:

    • 但请注意:System.Windows.Media.Imaging.BitmapSource 不能很好地处理线程。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    相关资源
    最近更新 更多