我必须使用DrawingContext.DrawImage方法绘制位图图像。
使用以下代码,一切正常:
BitmapImage myImage = new BitmapImage();
myImage.BeginInit();
myImage.UriSource = new Uri("image.png", UriKind.Relative);
myImage.EndInit();
Rect area = new Rect(new Size(myImage.PixelWidth, myImage.PixelHeight));
DrawingVisual myVisual = new DrawingVisual();
using (DrawingContext context = myVisual.RenderOpen())
{ context.DrawImage(myImage, area); }
但前提是图像不超过2Mb左右,即面积(myImage.PixelWidth x myImage.PixelHeight
(不大于10000x10000。在这种情况下,屏幕保持空白,并且不会引发任何异常(因此我无法判断是否存在错误(。
我该如何解决这个问题?谢谢。
渲染较大的图像时,它们似乎尚未加载。请尝试以下操作来加载位图:
BitmapSource myImage = BitmapFrame.Create(
new Uri("image.png"),
BitmapCreateOptions.None,
BitmapCacheOption.OnLoad);
或者这种绝望的尝试:
using (Stream fileStream = new FileStream("image.png", FileMode.Open))
{
BitmapSource myImage = BitmapFrame.Create(
fileStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}