1500x1500 image at 30fps on wpf
-
Hello, i'm moving my actual winform based application to WPF. My application acquire 1500x1500 8bit depth images at 30 frame per second. In winform i was used to BitBlt my images to a panel handle and that work fine. In wpf i tried two ways:
// on every frame:
int stride = img_w * ((PixelFormats.Gray8.BitsPerPixel + 7) / 8);
bmpSource = BitmapSource.Create(img_w, img_h, 96, 96, PixelFormats.Gray8, null, my_arrb_8bit, stride);this.Image1.Source = bmpSource;
but this is too slow...then i try
// on every frame:
int max = PixelFormats.Rgb24.BitsPerPixel;
uint count = (uint)(img_w * img_h * PixelFormats.Rgb24.BitsPerPixel / 8);
IntPtr section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, FileMapProtection.PageReadWrite, 0, count, null);
IntPtr map = MapViewOfFile(section, FileMapAccess.FileMapAllAccess, 0, 0, (UIntPtr)count);System.Runtime.InteropServices.Marshal.Copy(my_arrb_24bit, 0, map, (int)count);
my_ibs = System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection(
section,
(int)img_w,
(int)img_h,
PixelFormats.Rgb24,
(int)(img_w * PixelFormats.Rgb24.BitsPerPixel / 8), 0) as System.Windows.Interop.InteropBitmap;
this.Image1.Source = my_ibs;this is faster than the first solution, but still i can't manage to reach 30fps....am i doing something wrong? any suggestion?
-
Hello, i'm moving my actual winform based application to WPF. My application acquire 1500x1500 8bit depth images at 30 frame per second. In winform i was used to BitBlt my images to a panel handle and that work fine. In wpf i tried two ways:
// on every frame:
int stride = img_w * ((PixelFormats.Gray8.BitsPerPixel + 7) / 8);
bmpSource = BitmapSource.Create(img_w, img_h, 96, 96, PixelFormats.Gray8, null, my_arrb_8bit, stride);this.Image1.Source = bmpSource;
but this is too slow...then i try
// on every frame:
int max = PixelFormats.Rgb24.BitsPerPixel;
uint count = (uint)(img_w * img_h * PixelFormats.Rgb24.BitsPerPixel / 8);
IntPtr section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, FileMapProtection.PageReadWrite, 0, count, null);
IntPtr map = MapViewOfFile(section, FileMapAccess.FileMapAllAccess, 0, 0, (UIntPtr)count);System.Runtime.InteropServices.Marshal.Copy(my_arrb_24bit, 0, map, (int)count);
my_ibs = System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection(
section,
(int)img_w,
(int)img_h,
PixelFormats.Rgb24,
(int)(img_w * PixelFormats.Rgb24.BitsPerPixel / 8), 0) as System.Windows.Interop.InteropBitmap;
this.Image1.Source = my_ibs;this is faster than the first solution, but still i can't manage to reach 30fps....am i doing something wrong? any suggestion?
Off the top of my head, I'd suspect that the issue that you are hitting is because you are creating new BitmapSource elements each time. Basically, a WPF Bitmap is an immutable object - normally this is a good thing - but in the case where you want to keep changing a source, I'd suggest that you should look at a WriteableBitmap instead. This will give you some ability to mimic the BitBlt functionality.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
Off the top of my head, I'd suspect that the issue that you are hitting is because you are creating new BitmapSource elements each time. Basically, a WPF Bitmap is an immutable object - normally this is a good thing - but in the case where you want to keep changing a source, I'd suggest that you should look at a WriteableBitmap instead. This will give you some ability to mimic the BitBlt functionality.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierOn a first try using
writeableBitmap.Lock();
unsafe {
Marshal.Copy(my_arrb_8bit
, 0
, writeableBitmap.BackBuffer
, img_w * img_h);
}// Specify the area of the bitmap that changed.
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, img_w, img_h));// Release the back buffer and make it available for display.
writeableBitmap.Unlock();seems to start working not yet at 30fps but almost there...thank you very much :)
-
On a first try using
writeableBitmap.Lock();
unsafe {
Marshal.Copy(my_arrb_8bit
, 0
, writeableBitmap.BackBuffer
, img_w * img_h);
}// Specify the area of the bitmap that changed.
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, img_w, img_h));// Release the back buffer and make it available for display.
writeableBitmap.Unlock();seems to start working not yet at 30fps but almost there...thank you very much :)
You're welcome.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier