[英]How to properly render large bitmaps in WPF?
I did not expect 我没有期待
RenderTargetBitmap.Render(visual)
to have any side effects excerpt changing the bitmap data itself. 有任何副作用摘录改变位图数据本身。 It looks like it is not true. 看起来这不是真的。 I am unable to repeat it more than 60 times before some ugly rendering artifacts start to happen. 在一些丑陋的渲染工件开始发生之前,我无法重复超过60次。
How to properly render a lot of sprites in WPF? 如何在WPF中正确渲染很多精灵? Below is the code to reproduce the problem. 下面是重现问题的代码。
I generate sprites this way: 我用这种方式生成精灵:
BitmapSource Sprite()
{
var bitmap = new RenderTargetBitmap(
500, 500,
96, 96,
PixelFormats.Default);
var visual = new DrawingVisual();
var rect = new Rect(
new Size(
bitmap.Width,
bitmap.Height));
using (DrawingContext context = visual.RenderOpen())
context.DrawLine(
new Pen(Brushes.Red, 100),
rect.TopLeft,
rect.BottomRight);
bitmap.Render(visual);
bitmap.Freeze();
return bitmap;
}
Here is the canvas to render many of them: 这是渲染其中许多的画布:
public BitmapSource Canvas
{
get
{
var bitmap = new RenderTargetBitmap(
1980, 1080,
96, 96,
PixelFormats.Default);
var tiles = 70;
for (int i = 0; i < tiles; i++)
{
var visual = new DrawingVisual();
var rect = new Rect(
bitmap.Width / tiles * i,
0,
bitmap.Width / tiles,
bitmap.Height);
using (DrawingContext context = visual.RenderOpen())
context.DrawImage(Sprite(), rect);
bitmap.Render(visual);
}
bitmap.Freeze();
return bitmap;
}
}
I can see this strange picture while being data bound to Canvas property ... 在将数据绑定到Canvas属性时,我可以看到这张奇怪的图片......
Here is an example for using InteropBitmap
: 以下是使用InteropBitmap
的示例:
public InteropBitmapHelper(ColorSpace colorSpace, int bpp, int width, int height, uint byteCount)
{
_currentColorSpace = colorSpace;
_pixelFormat = GetPixelFormat(colorSpace, bpp);
if (_pixelFormat == PixelFormats.Rgb24 || _pixelFormat == PixelFormats.Rgb48 || _pixelFormat == PixelFormats.Bgr32 ||
_pixelFormat == PixelFormats.Bgr24 || _pixelFormat == PixelFormats.Bgr565 ||
_pixelFormat == PixelFormats.Gray16 || _pixelFormat == PixelFormats.Gray8)
{
int strideWidth = (width % 4 == 0) ? width : width - width % 4 + 4;
if (byteCount != strideWidth * height * (_pixelFormat.BitsPerPixel / 8))
{
strideWidth = width;
}
_stride = strideWidth * _pixelFormat.BitsPerPixel / 8;
_byteCount = (uint)((_stride) * height * ((short)bpp).NumberOfBytes());
ColorFileMapping = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, _byteCount, null);
if (ColorFileMapping == IntPtr.Zero)
{
var res=GetLastError();
IPDevLoggerWrapper.Error("Could not generate InteropBitmap "+res);
return;
}
ViewerImageData = MapViewOfFile(ColorFileMapping, 0xF001F, 0, 0, _byteCount);
InteropBitmap = Imaging.CreateBitmapSourceFromMemorySection(ColorFileMapping,
width,
height,
_pixelFormat,
_stride,
0) as InteropBitmap;
}
else
{
LoggerWrapper.Error("The image format is not supported");
return;
}
}
Here is how I connect it to the xaml. 这是我如何将它连接到xaml。
<Canvas
x:Name="content"
Width="{Binding ElementName=MainImage, Path=ActualWidth}"
Height="{Binding ElementName=MainImage, Path=ActualHeight}"
Background="Transparent"
MouseEnter="ImageMouseEnter"
MouseLeave="ImageMouseLeave"
RenderTransformOrigin ="0.5,0.5">
<Image x:Name="MainImage" Source="{Binding Source}" RenderOptions.BitmapScalingMode="{Binding ViewerRenderingMode }"/>
Please let me know if you need more info. 如果您需要更多信息,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.