secile / UsbCamera

C# source code for using usb camera and web camera in WinForms/WPF. With only single CSharp source code. No external library required.
MIT License
179 stars 55 forks source link

Implement IDisposable / IObservable pattern #24

Closed djonasdev closed 1 year ago

djonasdev commented 1 year ago

Since the use of events is rather an outdated technique, I would be happy if you would also implement the IObservable pattern in your library.

This would make it more than easy to use your library in conjunction with system.reactive. 🍻

secile commented 1 year ago

Hello. thank you for your suggestion.

I have no intention to do so at the present. Because by doing so, the library has dependency to the Reactive Extensions.

I have ever used the library with Reactive Extensions. I introduce you code snippet that adds Rx version of GetBitmap() to the library.

using System.Reactive.Linq;

public static class CameraRx
{
    public static IObservable<Bitmap> BitmapRx(this Camera camera, double millisecond)
    {
        return Observable.Interval(TimeSpan.FromMilliseconds(millisecond))
            .ObserveOn(System.Threading.SynchronizationContext.Current)
            .Select(x => camera.GetBitmap())
            .Where(x => x != null)
            .Publish().RefCount(); // cold to hot.
    }
}