Windows Phone 8.1活动中的图像并不清晰

时间:2014-07-08 10:31:40

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

我使用以下代码创建了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));

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

1 个答案:

答案 0 :(得分:1)

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

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