How to display quick-updating images without large memory allocation?

前端 未结 2 1849
后悔当初
后悔当初 2020-12-14 03:58

I\'ve got a WPF application on an ultrasound machine that displays ultrasound images generated in C++ at a speed upwards of 30 frames per second.

From what I underst

相关标签:
2条回答
  • 2020-12-14 04:15

    Here's some code I wrote* for aliasing (sharing memory) between a WPF BitmapSource and a GDI Bitmap (for my own project)

    Obviously, you'll need to tweak it for your own needs, it'll probably end up with a less "hacky" feel in the end.

    class AliasedBitmapSource : BitmapSource {
        private Bitmap source;
        public AliasedBitmapSource(Bitmap source) {
            this.source = source;
            this.pixelHeight = source.Height;
            this.pixelWidth = source.Width;
            this.dpiX = source.HorizontalResolution;
            this.dpiY = source.VerticalResolution;
        }
    
        public override event EventHandler DownloadCompleted;
        public override event EventHandler<ExceptionEventArgs> DownloadFailed;
        public override event EventHandler<ExceptionEventArgs> DecodeFailed;
    
        protected override Freezable CreateInstanceCore() {
            throw new NotImplementedException();
        }
    
        private readonly double dpiX;
        public override double DpiX {
            get {
                return dpiX;
            }
        }
    
        private readonly double dpiY;
        public override double DpiY {
            get {
                return dpiY;
            }
        }
    
        private readonly int pixelHeight;
        public override int PixelHeight {
            get {
                return pixelHeight;
            }
        }
    
        private readonly int pixelWidth;
        public override int PixelWidth {
            get {
                return pixelWidth;
            }
        }
    
        public override System.Windows.Media.PixelFormat Format {
            get {
                return PixelFormats.Bgra32;
            }
        }
    
        public override BitmapPalette Palette {
            get {
                return null;
            }
        }
    
        public unsafe override void CopyPixels(Int32Rect sourceRect, Array pixels, int stride, int offset) {
            BitmapData sourceData = source.LockBits(
            sourceRect.ToRectangle(),
            ImageLockMode.ReadWrite,
            System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    
            fixed (byte* _ptr = &((byte[])pixels)[0]) {
                byte* dstptr = _ptr;
                byte* srcptr = (byte*)sourceData.Scan0;
    
                for (int i = 0; i < pixels.Length; ++i) {
                    *dstptr = *srcptr;
                    ++dstptr;
                    ++srcptr;
                }
            }
    
            source.UnlockBits(sourceData);
        }
    }
    
    public static class Extensions {
        public static Rectangle ToRectangle(this Int32Rect me) {
            return new Rectangle(
            me.X,
            me.Y,
            me.Width,
            me.Height);
        }
    
        public static Int32Rect ToInt32Rect(this Rectangle me) {
            return new Int32Rect(
            me.X,
            me.Y,
            me.Width,
            me.Height);
        }
    }
    

    *by "wrote" I mean "threw together in 10 minutes"

    0 讨论(0)
  • 2020-12-14 04:21

    If you are using the lastest WPF bits check out WriteableBitmap, you'll have to do more of the leg work but you'll really fast updates.

    Do a quick google and you'll get some samples.

    0 讨论(0)
提交回复
热议问题