Blank screen using DrawImage method

放肆的年华 提交于 2019-12-11 01:35:10

问题


I have to draw a bitmap image using DrawingContext.DrawImage method.

Using the code below everything is working correctly:

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

But only if the image does not exceed 2Mb about, i.e. the area (myImage.PixelWidth x myImage.PixelHeight) is not larger than 10000x10000. In this case the screen remains blank and no any exception is thrown (so I can not tell if there was an error).

How could I fix this problem? Thanks.


回答1:


Looks like larger images aren't yet loaded when you render them. Try the following to load the bitmap:

BitmapSource myImage = BitmapFrame.Create(
    new Uri("image.png"),
    BitmapCreateOptions.None,
    BitmapCacheOption.OnLoad);

Or this desperate attempt:

using (Stream fileStream = new FileStream("image.png", FileMode.Open))
{
    BitmapSource myImage = BitmapFrame.Create(
        fileStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}


来源:https://stackoverflow.com/questions/10715485/blank-screen-using-drawimage-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!