2014-07-08 23 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; 
    } 
}