3

**
Found the solution
Because of the fact this is a tile, the image will always be strechted to 173 by 173!
To avoid this first create a dummy 173 by 173 and merge this with the resized one!

Rect rect = new Rect(0.0, 0.0, width, height);
WriteableBitmap bitmapDummy = new WriteableBitmap(173, 173);
bitmapDummy.Blit(rect, resized, rect, WriteableBitmapExtensions.BlendMode.None);

**

Well I have created a Background agent to update the live tile of my WP7 app. But no matter what I try to resize it, I'm not getting a good result!

Any tips? Currently I have following code, but I also tried 135 by 173 and also the other Interpolation.

WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
var resized = writeableBitmap.Resize(173, 173, System.Windows.Media.Imaging.WriteableBitmapExtensions.Interpolation.Bilinear);

There is also a small rectangle added below to show the title of the app! It's 40px in height, would be great if image would be cropped above. The actual image is always 250 by 321px

Current Tile

Actual image

Depechie
  • 6,102
  • 24
  • 46

1 Answers1

5

Your problem is that you're not calculating the width/heights to a correct Aspect ratio.

So to get a 1:1 proportions, you would need a width of 134.735 pixels, for a 173 pixel height.

This can be done by first determining what side is the largest

var aspect = Math.Max(bitmapImage.Width, bitmapImage.Height)
var ratio = largest / 173;
var width = width / ratio;
var height = height / ratio;

var resizedImage = writeableBitmap.Resize(width, height, System.Windows.Media.Imaging.WriteableBitmapExtensions.Interpolation.Bilinear);

And remember to set Stretch="Uniform" to avoid stretching the image to unnecessary proportions.

To create a 173x173 pixel image, with the other image applied on top, use the Blit function from WriteableBitmapEx

var tileImage = new WriteableBitmap(173, 173, ...)
tileImage.Blit(new Rect(width, height), resizedImage, new Rect(width, height), BlendMode.None);
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • Claus, thanks for some insight! I'll be testing soon! Thanks 'again' ;) – Depechie Jan 05 '12 at 14:25
  • Claus, nope doesn't work! I can't set any Stretch props, because of the fact that I'm just saving the image and use the uri for the Live Tile. – Depechie Jan 08 '12 at 23:21
  • My guess is I need to create a Dummy 173 by 173 and merge this with the newly resized one, this to avoid the stretching... now to find out how to do this – Depechie Jan 08 '12 at 23:22
  • Yes, the Live Tile will of course attempt to stretch the image, if it's not 173x173. But you would have to calculate the ratio regardless :p – Claus Jørgensen Jan 08 '12 at 23:34
  • Found it :) I'll add my solution to the question and indeed the ratio has to be correct ;) – Depechie Jan 08 '12 at 23:36
  • See the updated answer for how to use `Blit` to combine the images. – Claus Jørgensen Jan 08 '12 at 23:39