Taking screenshot in MAUI Blazor Hybrid application for Windows.

Jignesh Desai 106 Reputation points
2024-11-25T16:25:01.42+00:00

Hi

I am trying to Take screenshot in MAUI Blazor Hybrid application for Windows.

This is what i have used so far.

Home.razor.cs

[Inject] public Microsoft.Maui.Media.IScreenshot sshot { get; set; }
  public async Task btnscreen()
  {
      try
      {
          var screenshotResult = await sshot.CaptureAsync();
          if (screenshotResult != null)
          {
              // Convert the screenshot to a stream and then to a byte array
              using (var memoryStream = new MemoryStream())
              {
                  await screenshotResult.CopyToAsync(memoryStream, ScreenshotFormat.Png);
                  byte[] screenshotData = memoryStream.ToArray();
                  // Convert the byte array to a Base64 string
                  var base64Image = Convert.ToBase64String(screenshotData);
                  // Display the screenshot in the image tag using JavaScript Interop
                  await JSRuntime.InvokeVoidAsync("eval", $"document.getElementById('screenshotImage').src = 'data:image/png;base64,{base64Image}'");
              }
             }
      }
      catch (Exception ex)
      {
          Console.WriteLine($"Error capturing screenshot: {ex.Message}");
      }
  }

But what I get is following...

User's image

Can anyone guide me on this. Thanks in advance.

Regards

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,613 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,700 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,181 Reputation points Microsoft Vendor
    2024-11-26T05:55:52.54+00:00

    Hello

    This issue could be reproduced in my side.

    And reported in the github:Screenshot capture does not work for Blazor MAUI #9718

    .

    this appears to be an issue with WebView2 in WinUI, so PG logged this issue: MicrosoftEdge/WebView2Feedback#2799

    You can track this issue with above link.

    If you want to take a screenshot for windows in MAUI Blazor application.

    You can use Process.Start to ` new ProcessStartInfo

         {
    
             FileName = "SnippingTool.exe",
    
             UseShellExecute = true
    
         });` to open the SnippingTool.exe.
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.