I created a small wpf test application that displays some random rectangles every 30 ms using System.Drawing.Graphics and wpf InteropBitmap . I thought InteropBitmap would be faster than WriteableBitmap: it has the ability to update itself from a memory partition.
When running the application (screen size 1600 * 1200), the processor usage in the application is approximately 2-10% with dual-core 3GHz. But overall CPU usage is 80-90% , because the "System (NT Kernel and System)) process rises to 70%! EDIT: And I noticed that RAM usage periodically increases by more than 1 GB for 15 seconds, and then suddenly returns to normal levels, etc.
Maybe you can optimize the following code?
namespace InteropBitmapTest{
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Color = System.Drawing.Color;
public partial class Window1 : Window
{
private System.Drawing.Bitmap gdiBitmap;
private Graphics graphics;
InteropBitmap interopBitmap;
const uint FILE_MAP_ALL_ACCESS = 0xF001F;
const uint PAGE_READWRITE = 0x04;
private int bpp = PixelFormats.Bgr32.BitsPerPixel / 8;
private Random random;
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
SolidBrush[] brushes = new SolidBrush[] { new SolidBrush(Color.Lime), new SolidBrush(Color.White) };
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFileMapping(IntPtr hFile,
IntPtr lpFileMappingAttributes,
uint flProtect,
uint dwMaximumSizeHigh,
uint dwMaximumSizeLow,
string lpName);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject,
uint dwDesiredAccess,
uint dwFileOffsetHigh,
uint dwFileOffsetLow,
uint dwNumberOfBytesToMap);
public Window1()
{
InitializeComponent();
Loaded += Window1_Loaded;
WindowState = WindowState.Maximized;
timer.Tick += timer_Tick;
timer.Interval = 30;
random = new Random();
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
CreateBitmaps();
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
int width = 50;
for (int i = 0; i < 100; i++)
{
int left = random.Next((int)(ActualWidth - width));
int top = random.Next((int)(ActualHeight - width));
graphics.FillRectangle(brushes[left % 2], left, top, width, width);
}
interopBitmap.Invalidate();
}
void CreateBitmaps()
{
uint byteCount = (uint) (ActualWidth * ActualHeight * bpp);
var sectionPointer = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, PAGE_READWRITE, 0, byteCount, null);
var mapPointer = MapViewOfFile(sectionPointer, FILE_MAP_ALL_ACCESS, 0, 0, byteCount);
var format = PixelFormats.Bgr32;
interopBitmap = Imaging.CreateBitmapSourceFromMemorySection(sectionPointer, (int)ActualWidth, (int)ActualHeight, format,
(int)(ActualWidth * format.BitsPerPixel / 8), 0) as InteropBitmap;
gdiBitmap = new System.Drawing.Bitmap((int)ActualWidth, (int)ActualHeight,
(int)ActualWidth * bpp,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
mapPointer);
graphics = Graphics.FromImage(gdiBitmap);
wpfImage.Source = (BitmapSource) interopBitmap;
}
}}
XAML:
<Window x:Class="InteropBitmapTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Image Name="wpfImage" Stretch="None" />
</Window>