尝试创建WriteableBitmap实例时WP7崩溃

时间:2012-05-01 07:25:00

标签: windows-phone-7

每个人,我在将图片保存到媒体库时遇到了一个奇怪的问题,我的应用程序在没有异常情况下崩溃了。这是我的保存代码。

using (MemoryStream stream = new MemoryStream())
{
    try
    {
        WriteableBitmap bitmap = new WriteableBitmap(InkPrest, InkPrest.RenderTransform); // Crash here, the actualHeight of InkPrest is 2370.0 

        bitmap.SaveJpeg(stream, (int)InkPrest.ActualWidth, (int)InkPrest.ActualHeight, 0, 100);
        stream.Seek(0, SeekOrigin.Begin);

        MediaLibrary library = new MediaLibrary();
        library.SavePicture(DateTime.Now.ToString(), stream.GetBuffer());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我已经逐步进行了调试,应用程序崩溃了

WriteableBitmap bitmap = new WriteableBitmap(InkPrest, InkPrest.RenderTransform); // Crash here, the actualHeight of InkPrest is 2370.0

有任何关于解决这个问题的想法吗?

============================================

尝试保存多张图片

该元素是704 * 2370

TranslateTransform transform = new TranslateTransform();
transform.Transform(new Point(0,0));
double MaxHeight = 800;
double height = InkPrest.ActualHeight;
int saveCount = 0;
int succeedCount = 0;
while (height > 0)
{
    using (MemoryStream stream = new MemoryStream())
    {
        try
        {
            double actualRenderHeight = Math.Min(height, MaxHeight);
            WriteableBitmap bitmap = new WriteableBitmap((int)InkPrest.ActualWidth, (int)actualRenderHeight);

            bitmap.Render(InkPrest, transform); //Crash here, also no exception.
            bitmap.Invalidate();

            height -= actualRenderHeight;
            transform.Y -= actualRenderHeight;

            bitmap.SaveJpeg(stream, (int)InkPrest.ActualWidth, (int)actualRenderHeight, 0, 100);
            stream.Seek(0, SeekOrigin.Begin);

            MediaLibrary library = new MediaLibrary();
            Picture pic = library.SavePicture(manuscriptFile.Title + DateTime.Now.ToString(), stream.GetBuffer());
            saveCount++;
            if (pic != null)
            {
                succeedCount++;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

检查是否在uithread或其他一些已创建的线程中实例化writeablebitmap。你需要在uithread中创建writeablebitmap。