问题
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