【问题标题】:Images in Windows Phone 8.1 livetiles aren't sharpWindows Phone 8.1 livetiles 中的图像不清晰
【发布时间】:2014-07-09 20:29:34
【问题描述】:

我用下面的代码创建了 livetiles:

// wide 310x150
var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150PeekImage03);
tileXml.GetElementsByTagName(textElementName).LastOrDefault().InnerText = string.Format(artist + " - " + trackname);
var image = tileXml.GetElementsByTagName(imageElementName).FirstOrDefault();
if (image != null)
{
    var src = tileXml.CreateAttribute("src");
    if (albumart == String.Empty)
        src.Value = "Assets/onemusic_logo_wide.scale-240.png";
    else
        src.Value = albumart;
    image.Attributes.SetNamedItem(src);
}

// square 150x150
var squaredTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150PeekImageAndText01);
squaredTileXml.GetElementsByTagName(textElementName).FirstOrDefault().InnerText = string.Format(artist + " - " + trackname);
image = squaredTileXml.GetElementsByTagName(imageElementName).LastOrDefault();
if (image != null)
{
    var src = squaredTileXml.CreateAttribute("src");
    if (albumart == String.Empty)
        src.Value = "Assets/onemusic_logo_square.scale-240.png";
    else
        src.Value = albumart;
    image.Attributes.SetNamedItem(src);
}

updater.Update(new TileNotification(tileXml));
updater.Update(new TileNotification(squaredTileXml));

我面临的问题是 livetile 上显示的图像不清晰(在应用程序中它们是)。我认为这是因为模板的大小为 310x150 像素。我查看了模板,没有更高分辨率的模板。有没有办法让图像更清晰?

【问题讨论】:

  • 您需要避免强制手机调整您的磁贴大小。这让它变得模糊。 msdn.microsoft.com/en-us/library/windowsphone/develop/…
  • 基本上,在大多数情况下,缩小规模是可以的。 310x150 仅表示该图块是宽图块 - 与实际分辨率无关。在某些设备上,宽图块的分辨率为 691x336,因此您需要有这么大的图像,它适用于所有较小的分辨率。

标签: c# windows-8 windows-8.1 windows-phone-8.1


【解决方案1】:

我注意到提供分辨率正好为 744x360 像素的图像可以解决问题。所以我写了这个函数来调整我的专辑大小(也许它会派上用场);

private async static Task<string> CropAndSaveImage(string filePath)
{
    const string croppedimage = "cropped_albumart.jpg";

    // read file
    StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);
    if (file == null)
        return String.Empty;

    // create a stream from the file and decode the image
    var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

    // create a new stream and encoder for the new image
    using (InMemoryRandomAccessStream writeStream = new InMemoryRandomAccessStream())
    {
        // create encoder
        BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(writeStream, decoder);
        enc.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;

        // convert the entire bitmap to a 744px by 744px bitmap
        enc.BitmapTransform.ScaledHeight = 744;
        enc.BitmapTransform.ScaledWidth = 744;
        enc.BitmapTransform.Bounds = new BitmapBounds()
        {
            Height = 360,
            Width = 744,
            X = 0,
            Y = 192
        };

        await enc.FlushAsync();

        StorageFile albumartfile = await ApplicationData.Current.LocalFolder.CreateFileAsync(croppedimage, CreationCollisionOption.ReplaceExisting);
        using (var stream = await albumartfile.OpenAsync(FileAccessMode.ReadWrite))
        {
            await RandomAccessStream.CopyAndCloseAsync(writeStream.GetInputStreamAt(0), stream.GetOutputStreamAt(0));
        }

        // return image path
        return albumartfile.Path;
    }
}

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多