2

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

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

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

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

尝试保存多张图片

uielement 为 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);
        }
    }
}
4

1 回答 1

0

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

于 2012-09-03T12:43:40.717 回答