C#学习教程:WPF:如何高效地每秒更新图像30次我事先知道位图是24bitrgb,它的宽度和高度。使用这些位图更新图像控件为用户制作视频,但我不确定最有效的方法是什么,大多数时候CPU使用率达到75%+,内存从40mb到500mbnI认为GC开始,然后再次下降到40毫米。应用程序开始无响应。我该怎么办?谢谢!您很可能正在分配新的位图,这些位图不是一次性的。您应该分配一个WriteableBitmap并更新它。链接的文档描述了锁定、更新和解锁WriteableBitmap在我用于WPF中的实时超声图像的软件上,我收到了一个Windows窗体位图,我使用本机CopyMemory方法将其直接复制到WriteableBitmap中。即使进行这项更复杂的工作,CPU也不会过度紧张,只要我的内存正确,内存使用率就不会改变。希望这个例子对你有帮助://DLLreturnsimagesasaWinFormsBitmapBitmapbmp=myClass.getWinFormsBitmap();//在我的情况下,图像始终为640x480。BitmapDatadata=bmp.LockBits(newRectangle(0,0,640,480),ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb);这个.writeableBitmap.Lock();//将位图数据直接复制到屏幕缓冲区NativeMethods.CopyMemory(this.writeableBitmap.BackBuffer,data.Scan0,??ImageBufferSize);//将后台缓冲区移到前台。this.writeableBitmap.AddDirtyRect(newInt32Rect(0,0,640,480));this.writeableBitmap.Unlock();bmp。解锁位(数据);//释放WinForms位图的内存bmp.Dispose();CopyMemory定义为:[DllImport("Kernel32.dll",EntryPoint="RtlMoveMemory")]publicstaticexternvoidCopyMemory(IntPtrDestination,IntPtrSource,intLength);在WriteableBitmap上使用一个叫做WritePixels的便捷方法,我们可以写得更短,如下:我希望你会更多地关注教程—//DLL将图像作为WinForms位图返回//即使使用(Bitmapbmp=myClass.getWinFormsBitmap())抛出异常,它也会被处理{//在我的情况下,图像总是640x480。BitmapDatadata=bmp.LockBits(newRectangle(0,0,640,480),ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb);writeableBitmap.WritePixels(newInt32Rect(0,0,640,480),data.Scan0,??ImageBufferSize,data.Stride);bmp.UnlockBits(数据);}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
