缩放1920 * 1080p照片成为1080p Windows Phone上的锁定屏幕图像

问题描述:

我想使用当天的Bing图像作为我的应用程序的锁定屏幕图像的背景,但我在将图像适合在1080p设备上进行缩放。缩放1920 * 1080p照片成为1080p Windows Phone上的锁定屏幕图像

这是一天的1080p Bing图像示例:http://www.bing.com//az/hprichbg/rb/BeaverMeadow_EN-US12190942812_1920x1080.jpg。这是一张1920 * 1080的照片。

我要做的就是庄稼它,这样我使用的照片是1080个* 1080个像素,然后创建一个新的锁屏图像这是1080 * 1920。这是代码:

public static void SaveToJpeg(Stream stream) 
    { 
     using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (IsolatedStorageFileStream isostream = iso.CreateFile("lockscreen.jpg")) 
      { 
       try 
       { 
        BitmapImage bitmap = new BitmapImage(); 
        bitmap.SetSource(stream); 
        WriteableBitmap wb = new WriteableBitmap(bitmap); 

        // Cropping image so that only 1080 out of the 1920 horizontal pixels are used. 
        wb = CropImage(wb, 1080, 1920, 1080, 1080); 

        // 1080 * 1920 are the phone's dimesions. 
        Extensions.SaveJpeg(wb, isostream, 1080, 1920, 0, 100); 
        isostream.Close(); 
       } 
       catch(Exception e) 
       { 
       } 
      } 
     } 
    } 

    public static WriteableBitmap CropImage(WriteableBitmap source, int phoneWidth, int phoneHeight, 
                int width, int height) 
    { 

     // Based on the phone's width/height and image's width/height, will determine 
     // the correct x and y offsets. 
     int xOffset = 0, yOffset = 0; 

     if(phoneWidth >= source.PixelWidth) 
     { 
      xOffset = 0; 
     } 
     else 
     { 
      xOffset = source.PixelWidth - phoneWidth; 
      xOffset = xOffset/2 + xOffset/4; 
     } 

     if (phoneHeight >= height) 
     { 
      yOffset = 0; 
     } 
     else 
     { 
      yOffset = height - phoneHeight; 
      yOffset = yOffset/2; 
     } 


     var sourceWidth = source.PixelWidth; 

     // Get the resultant image as WriteableBitmap with specified size 
     var result = new WriteableBitmap(width, height); 

     // Create the array of bytes 
     for (var x = 0; x <= height - 1; x++) 
     { 
      var sourceIndex = xOffset + (yOffset + x) * sourceWidth; 
      var destinationIndex = x * width; 

      Array.Copy(source.Pixels, sourceIndex, result.Pixels, destinationIndex, width); 
     } 
     return result; 
    } 

不出所料,鉴于兵形象的高度为1080个像素(而不是1920),这是锁屏的样子:

enter image description here

而且,没错,就是锁屏图像从创建自定义的用户控件其网格背景图像拉伸到fi ll:

 <Grid.Background> 
      <ImageBrush 
       x:Name="Background" 
       Stretch="Fill"/> 
     </Grid.Background> 

我需要做些什么才能让Bing图像优雅地填满屏幕?也就是说,我不想过分调整(像素化)原始图像的大小以匹配1080p手机的尺寸。

更新:我为当天Bing图像找到了一个替代1080 x 1920的照片(即,具有1080p手机锁定屏幕的确切尺寸):http://www.bing.com//az/hprichbg/rb/BeaverMeadow_EN-US12190942812_1080x1920.jpg

然而,使用它似乎并没有解决潜在的问题(注意:我没有裁剪任何东西从这个图像,但使用这个图像,因为它是因为尺寸是完美的)。请看下图:

enter image description here

好吧,这是愚蠢的。在锁屏的用户控件的XAML我不得不提高电网的最后一行的高度:

 <Grid.RowDefinitions> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="0"/> 
      <RowDefinition Height="30"/> 
      <RowDefinition Height="160"/> 
      <RowDefinition Height="18"/> 
      <RowDefinition Height="1920"/> 
     </Grid.RowDefinitions> 

它之前设置为900