【问题标题】:Creating Bitmap Image or Memory stream from xaml control using Sharpdx library(WinRT)使用 Sharpdx 库 (WinRT) 从 xaml 控件创建位图图像或内存流
【发布时间】:2012-07-24 09:51:37
【问题描述】:

是否可以从 Sharpdx 创建位图图像或 xaml 控件的内存流 say(gird, canvas)。我需要从我的一个窗口创建一个图像,以实现辅助磁贴 Pin 启动功能。

【问题讨论】:

标签: windows-8 windows-runtime sharpdx


【解决方案1】:

我一直在使用 Windows.Storage.Streams 中的 RandomAccessStreamReference 类来创建位图。一个例子(这实际上是分享的代码):

        var reference = RandomAccessStreamReference.CreateFromUri(new Uri(item.ImagePath.AbsoluteUri));
        request.Data.SetBitmap(reference);

另外请记住,对于固定辅助磁贴,您可以为磁贴上的徽标传入一个 URI,而无需创建实际的位图,只要徽标是您的应用程序包的一部分,如下所示:

    var uri = new Uri(item.TileImagePath.AbsoluteUri);

    var tile = new SecondaryTile(
            item.UniqueId,              // Tile ID
            item.ShortTitle,            // Tile short name
            item.Title,                 // Tile display name
            item.UniqueId,              // Activation argument
            TileOptions.ShowNameOnLogo, // Tile options
            uri                         // Tile logo URI
        );

    await tile.RequestCreateAsync();

最后,如果您要在辅助磁贴上使用的图像是在线的,而不是应用程序包的一部分,则您必须先将其复制到本地,然后才能使用它。这是一些执行此操作的代码:

    // This is the URI that you will then pass as the last parameter into 
    // the Secondary Tile constructor, like the code above:
    var logoUri = await GetLocalImageAsync(restaurant.ImagePath, restaurant.Key);

    // and here's the method that does the meat of the work:

/// <summary>
/// Copies an image from the internet (http protocol) locally to the AppData LocalFolder.
/// This is used by some methods (like the SecondaryTile constructor) that do not support 
/// referencing images over http but can reference them using the ms-appdata protocol.  
/// </summary>
/// <param name="internetUri">The path (URI) to the image on the internet</param>
/// <param name="uniqueName">A unique name for the local file</param>
/// <returns>Path to the image that has been copied locally</returns>
private async Task<Uri> GetLocalImageAsync(string internetUri, string uniqueName)
{
    if (string.IsNullOrEmpty(internetUri))
    {
        return null;
    }

    using (var response = await HttpWebRequest.CreateHttp(internetUri).GetResponseAsync())
    {
        using (var stream = response.GetResponseStream())
        {
            var desiredName = string.Format("{0}.jpg", uniqueName);
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceExisting);

            using (var filestream = await file.OpenStreamForWriteAsync())
            {
                await stream.CopyToAsync(filestream);
                return new Uri(string.Format("ms-appdata:///local/{0}.jpg", uniqueName), UriKind.Absolute);
            }
        }
    }
}

【讨论】:

  • 这不是我期望得到的答案。在这里,您演示了创建 PinToTile 的方法。我被问到的是不同的东西。我没有可以用手固定的图像;我的要求是,我有一个带有一些数据的控件(比如网格或画布),我需要从该控件创建一个图像,然后我需要将该图像固定到开始屏幕。希望你能理解我的问题。我可以在 wp7 和 wpf 中使用 WritableBitmap.Render () 方法从控件创建图像。但在 WINRT 中没有。我需要解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多