<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Happy Coding🚀Happy Learning]]></title><description><![CDATA[Happy Coding🚀Happy Learning]]></description><link>https://prasadbdissanayake.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 09 Sep 2026 08:28:39 GMT</lastBuildDate><atom:link href="https://prasadbdissanayake.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Building a WPF Camera Preview App with DirectShowLib and C#]]></title><description><![CDATA[Introduction:
WPF does not include a native control for displaying live camera feeds, which can be challenging when you need to integrate webcam functionality into your desktop application. Whether you are working with a built-in laptop camera or an ...]]></description><link>https://prasadbdissanayake.hashnode.dev/building-a-wpf-camera-preview-app-with-directshowlib-and-c</link><guid isPermaLink="true">https://prasadbdissanayake.hashnode.dev/building-a-wpf-camera-preview-app-with-directshowlib-and-c</guid><category><![CDATA[wpf]]></category><category><![CDATA[C#]]></category><category><![CDATA[.NET Framework]]></category><category><![CDATA[camera]]></category><dc:creator><![CDATA[Prasad Dissanayake]]></dc:creator><pubDate>Mon, 26 Jan 2026 17:34:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769430551291/16478061-1ebe-4fd4-a9e1-753dbec57044.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Introduction:</strong></p>
<p>WPF does not include a native control for displaying live camera feeds, which can be challenging when you need to integrate webcam functionality into your desktop application. Whether you are working with a built-in laptop camera or an external USB webcam like a Logitech device, you need a reliable solution to capture and display video streams.</p>
<p>In this article, I will walk you through a simple WPF demo that uses DirectShowLib to display a live camera preview. This approach also allows you to extend the functionality for capturing photos or recording video streams.</p>
<p><strong>The Challenge: No Native Camera Control in WPF</strong></p>
<p>Unlike some frameworks, WPF does not provide a built-in control for a camera or video preview. To work around this limitation, we use <code>WindowsFormsHost</code> to embed a Win32 window handle, which DirectShowLib requires for rendering the video feed.</p>
<p><strong>Solution Overview</strong></p>
<p>This demo demonstrates how to:</p>
<ul>
<li><p>Access cameras connected to your PC (built-in webcams or USB cameras)</p>
</li>
<li><p>Display a live camera preview using DirectShowLib</p>
</li>
<li><p>Structure the code for maintainability and potential MVVM migration</p>
</li>
</ul>
<p><strong>Architecture and Design</strong></p>
<p>To keep the codebase clean and maintainable, I have separated concerns:</p>
<ul>
<li><p><strong>UI Layer (Code-Behind):</strong> Handles the WindowsFormsHost wiring, as the camera preview requires a window handle</p>
</li>
<li><p><strong>Service Layer:</strong> Contains the DirectShowLib camera graph logic, isolated from the UI</p>
</li>
</ul>
<p>This separation ensures the core camera functionality is testable and can be refactored into a full MVVM pattern if needed in the future.</p>
<p><strong>Key Implementation Details</strong></p>
<p>The camera preview is hosted using <code>WindowsFormsHost</code> because DirectShowLib needs a window handle (HWND) to render the video stream. While this requires some UI logic in the code-behind, the camera graph setup and management remain decoupled in a dedicated service class.</p>
<p>This design makes it straightforward to extend the application with additional features such as:</p>
<ul>
<li><p>Capturing still images from the video stream</p>
</li>
<li><p>Recording video to a file</p>
</li>
<li><p>Switching between multiple connected cameras</p>
</li>
</ul>
<p><strong>Conclusion</strong></p>
<p>This simple WPF demo shows how to overcome the lack of native camera support in WPF by leveraging DirectShowLib and WindowsFormsHost. The modular design keeps the code maintainable while providing a solid foundation for building more advanced camera functionality.</p>
<p>Feel free to explore the code and adapt it to your own projects. If you have questions or suggestions, I would love to hear them in the comments!  </p>
<p>💻 GitHub repo: <a target="_blank" href="https://github.com/prasadbdissanayake/wpf-camera-csharp">https://github.com/prasadbdissanayake/wpf-camera-csharp</a></p>
]]></content:encoded></item></channel></rss>