Search code examples
imagebackgroundwindows-phone-8lockscreen

Windows Phone 8 - Generating Lockscreen Image in the Background


I'm trying to create a Windows Phone 8 app (update of my currently published "The Quote") that uses the new Windows Phone 8 Live Lockscreen API. I basically want to choose randomly a image background from the app package and place a textblock on it with a random quote to create the lockscreen image. How can I accomplish that in the background periodic task? There is definitely a way to do it (many current apps including different weather and news apps create live lockscreen locally in the background), but I just don't seem to be able to find out how and so far no internet search got me anything useful.

Any help is very appreciated!

Thank you very much!

EDIT:

I was able to find a way to create a UserControl with my content and take a screenshot of it this way:

var bmp = new WriteableBitmap(768, 1280);
bmp.Render(LayoutRoot, null);

String tempJPEG = "TempJPEG.jpg";

var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempJPEG))
{
    myStore.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);

WriteableBitmap wb = new WriteableBitmap(bmp);

wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
myFileStream.Close();

This approach got me three different problems:

  1. If I didn't set the WriteableBitmap's size in constructor, it chose it incorrectly and the lockscreen was useless.

  2. If I run the code above, it throws an OutOfMemory error

  3. In the 1 case, there was also a problém with the Control's background (went black, even though I have set the main Grid's Background brush to ImageBrush linking to a local file from the main Appx package.

Is this completely wrong? Is there a better (working) way?

Thank you all so much, I appreciate your help.


Solution

  • You are most likely running into Memory cap limit in your background agent, which is 11 MB on WP8. I would recommend you to render your images on server/Azure and just download it in Background agent, save it into phone and display it on lockscreen, or maybe to use Resource Intesive Task for the rendering?
    I use tile rendering in one of my apps and I ran into memory cap when I tried to render just 2 tile images of size 336x336 + 159x159px, so you can imagine rendering 768x1280 image could easily reach this cap as well.