WriteableBitmap崩溃程序没有消息?

时间:2013-06-19 22:52:20

标签: c# wpf writeablebitmap writeablebitmapex

我创建了一个WriteableBitmap对象,绘制一条线并尝试将其设置为Image控件的源。出于某种原因,程序停止响应,然后在我尝试设置Source时5秒后关闭。任何人都知道什么是错的? (我也在使用WriteableBitmapEx)

WriteableBitmap bit = new WriteableBitmap(400, 400, 96, 96, PixelFormats.Bgr32, null);
WriteableBitmapExtensions.DrawLine(bit, 10, 10, 300, 300, Core.PrimaryColor.ColorValue);
ImageCanvas.Source = bit; // Sets the image to our bitmap, but program crashes for some reason!

1 个答案:

答案 0 :(得分:3)

当我尝试你的代码时,会抛出一个ArgumentException说

  

输入WriteableBitmap需要具有Pbgra32像素格式。使用   BitmapFactory.ConvertToPbgra32Format方法自动生成   将任何输入的BitmapSource转换为此接受的正确格式   class。\ r \ nParametername:writeableBitmap

因此这有效:

var bitmap = new WriteableBitmap(400, 400, 96, 96, PixelFormats.Pbgra32, null);
WriteableBitmapExtensions.DrawLine(bitmap, 10, 10, 300, 300, Colors.Black);
image.Source = bitmap;

更新:如Anders所述,您应该使用WriteableBitmapEx提供的便携式位图工厂方法来创建位图:

var bitmap = BitmapFactory.New(400, 400);
WriteableBitmapExtensions.DrawLine(bitmap, 10, 10, 300, 300, Colors.Black);
image.Source = bitmap;