开发者

Rendering a usercontrol to an image in the MediaLibrary

private void SaveAsPicture_Click(object sender, Rout开发者_Python百科edEventArgs e)
{
    WriteableBitmap bmp = new WriteableBitmap(MyUIElement, null);
    var library = new MediaLibrary();
    MemoryStream stream = new MemoryStream();
    bmp.SaveJpeg(stream, 100, 100, 0, 90);
    library.SavePicture("Certificate", stream);
}

This should save the rendering of the MyUIElement to a bmp then save that as a Jpeg in the medialibrary but i'm getting a value does not fall within expected range error on the line with library.SavePicture("Certificate", stream);

Any ideas?


I got the same error like the one you had. And I solved it by following the example on How to: Encode a JPEG for Windows Phone and Save to the Pictures Library on MSDN.

So your method should look as following

private void SaveAsPicture_Click(object sender, RoutedEventArgs e)
{
    WriteableBitmap bmp = new WriteableBitmap(MyUIElement, null);
    library.SavePicture("Certificate", stream);
    String tempJPEG = "TempJPEG";

    // Create a virtual store and file stream. Check for duplicate tempJPEG files.
    var myStore = IsolatedStorageFile.GetUserStoreForApplication();
    if (myStore.FileExists(tempJPEG))
    {
        myStore.DeleteFile(tempJPEG);
    }

    IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);

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

    // Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
    myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);

    // Save the image to the camera roll or saved pictures album.
    MediaLibrary library = new MediaLibrary();

        // Save the image to the camera roll album.
    library.SavePicture("Certificate", myFileStream);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜