2

我必须使用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。在这种情况下,屏幕保持空白并且没有抛出任何异常(所以我无法判断是否有错误)。

我该如何解决这个问题?谢谢。

4

1 回答 1

2

渲染它们时,看起来更大的图像尚未加载。尝试以下方法加载位图:

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);
}
于 2012-05-23T11:11:38.580 回答