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);
}
}
}
答案 0 :(得分:0)
检查是否在uithread或其他一些已创建的线程中实例化writeablebitmap。你需要在uithread中创建writeablebitmap。