Skip to content

Take Photos with PhotoCaptureView on Windows, Android and iOS

Try it Download Windows trial dotnet add package VisioForge.DotNet.VideoCapture Pricing →

PhotoCaptureView (namespace VisioForge.Core.PhotoCapture) is the camera control for still photos in .NET MAUI and native mobile apps. It lists the cameras, starts a preview at a resolution you choose, and returns each photo as an SKBitmap or saves it to a file. It runs on Windows (a Windows 10 target framework such as net10.0-windows10.0.19041.0), Android and iOS; Mac Catalyst is not supported.

For photos from a webcam in a Windows desktop app built with VideoCaptureCore, see Webcam Photo Capture.

Get the camera list

On Windows the constructor does not block: the cameras are enumerated in the background, which takes about a second (longer with more cameras attached), and Cameras stays empty until that finishes. Await GetCamerasAsync() before you read the list. On Android and iOS the list is ready at once, and GetCamerasAsync() returns it immediately, so the code after the constructor is the same on every platform (the constructor itself takes an Android Context or an iOS CGRect frame there).

using VisioForge.Core.PhotoCapture;

// Windows. On Android: new PhotoCaptureView(context); on iOS: new PhotoCaptureView(frame).
var photoView = new PhotoCaptureView();

// Waits for the background enumeration on Windows; returns at once on Android and iOS.
var cameras = await photoView.GetCamerasAsync();
if (cameras.Count == 0)
{
    return; // no camera
}

photoView.Camera = cameras[0];

StartCameraAsync also waits for the enumeration, but you need the list to pick Camera first.

Each CameraInfo carries Name, Position (front, back or unknown), HasFlashUnit, the zoom range and AvailableResolutions. On Windows, when a device's capabilities cannot be read (for example a capture card that refuses the probe, or a camera another application holds exclusively), the camera is still listed, its AvailableResolutions is empty, and the device error with its HRESULT is written to the log. No resolutions are invented for it.

Choose the resolution

Pass one of the camera's AvailableResolutions to StartCameraAsync, or pass nothing:

using VisioForge.Core.Types;

// Explicit: must be one of photoView.Camera.AvailableResolutions.
var result = await photoView.StartCameraAsync(new Size(1920, 1080));

// Or let the SDK choose.
// var result = await photoView.StartCameraAsync();

if (result != CameraResult.Success)
{
    // AccessDenied, ResolutionNotAvailable, AccessError, NoCameraSelected, ...
}

Without a resolution, the SDK takes the first 1920-pixel-wide entry of AvailableResolutions, and otherwise the first entry of the list (on Windows the list is sorted largest first). If the list is empty, the camera starts in its current mode. A resolution that is not in the list returns CameraResult.ResolutionNotAvailable.

Photo stream and preview stream on Windows

Some cameras have a photo stream of their own, separate from the preview: the built-in cameras of Surface tablets, for example. Most USB webcams have only one stream, shared by the preview and the photos. PhotoCaptureView handles both:

Camera Where the photo comes from Resolution of the photo
Has its own photo stream The photo stream; the preview keeps its own mode The selected photo resolution (for example 3264x2448 on a Surface Go rear camera)
One shared stream (most USB webcams) The preview stream The selected resolution; the preview also runs at it

AvailableResolutions lists the photo resolutions of the stream the photos are taken from; when that is the preview stream, only the sizes the camera can stream uncompressed are listed, so every entry can be started. When the photos come from the preview stream, an information message in the log says so. On such cameras a high resolution also means a larger preview frame and, on some cameras, a lower preview frame rate; choose the resolution with that in mind.

On Android and iOS the photo is always taken by the platform's still-capture pipeline at the selected resolution.

Take and save a photo

using SkiaSharp;

// As an SKBitmap: you own it and must dispose it.
using (var photo = await photoView.TakePhotoAsync())
{
    if (photo != null)
    {
        // photo.Width x photo.Height is the resolution of the photo stream.
    }
}

// Or straight to a file (JPEG, quality 85, saved to the device's picture gallery by default).
bool saved = await photoView.SavePhotoAsync("photo.jpg");

Stop the camera with StopCameraAsync() and call Dispose() when the view is no longer needed.

Use it in a .NET MAUI page

Register the VisioForge handlers in MauiProgram.cs with .ConfigureMauiHandlers(handlers => handlers.AddVisioForgeHandlers()), put a VisioForge.Core.UI.MAUI.PhotoCaptureView on the page, and work with the core view its handler creates:

using VisioForge.Core.UI.MAUI;

var core = (photoCaptureView.Handler as PhotoCaptureViewHandler).Core;

var cameras = await core.GetCamerasAsync();
if (cameras.Count > 0)
{
    core.Camera = cameras[0];
    await core.StartCameraAsync();
}

Sample applications