2014-07-08 21 views
1

我创建了下面的代码livetiles:图片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)); 

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

+0

您需要避免强制手机调整拼贴大小。这使它模糊。 http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202948%28v=vs.105%29.aspx#BKMK_Tilesizesandresolutions –

+0

基本上,在大多数情况下,缩小比例是可以的。 310x150只是意味着该瓷砖是一块宽阔的瓷砖 - 与实际分辨率无关。在某些设备上,宽瓦的分辨率为691x336,因此您需要有一个很大的图像,并且它适用于所有较小的分辨率。 – yasen

回答

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