2012-05-23 29 views
2

我必须使用DrawingContext.DrawImage方法绘制位图图像。使用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大。在这种情况下,屏幕保持空白,并且不会引发任何异常(所以我不能分辨是否有错误)。

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

+0

你检查,如果“大”图像装入正确,是否符合预期,即PixelWidth和PixelHeight? – Clemens

+0

有效当我绘制“大”图像PixelWidth和PixelHeight是1 ... – gliderkite

回答

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); 
} 
+0

PixelWidth和PixelHeight始终为1 – gliderkite

+0

请参阅编辑。如果它仍然不起作用,我会马上删除它。 – Clemens

+0

我不能使用CopyTo,我正在研究3.5 .NET框架,任何替代方案? – gliderkite