将用户控件渲染到MediaLibrary中的图像

时间:2011-09-13 12:09:24

标签: windows-phone-7

private void SaveAsPicture_Click(object sender, RoutedEventArgs 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);
}

这应该将MyUIElement的渲染保存到bmp然后将其保存为medialibrary中的Jpeg但我在value does not fall within expected range

的行上出现library.SavePicture("Certificate", stream);错误

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我得到了与你的错误相同的错误。我按照How to: Encode a JPEG for Windows Phone and Save to the Pictures Library on MSDN上的示例解决了这个问题。

因此,您的方法应如下所示

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);
}