Skip to content

[Bug] Microsoft.Maui.Graphics Blend modes unimplemented on Android and WindowsΒ #14945

Open

Description

@davidbritch

Microsoft.Maui.Graphics blend modes are unimplemented on Android and Windows.

Activity

  1. matt-goldman commented on Nov 20, 2022

    @matt-goldman
    Contributor

    @jsuarezruiz looks like this is not getting any attention. I've been waiting for this for a while but wondering at this stage:

    1. Should this be removed from the documentation for now? (i.e. https://learn.microsoft.com/en-us/dotnet/maui/user-interface/graphics/blendmodes?view=net-maui-7.0)
    2. Should I be patient and wait for this, or go with SkiaSharp?

    Note: This is for a sample for my upcoming book .NET MAUI in Action. My intention is to use this to replicate the switch control seen in Outlook (for context, see: https://goforgoldman.com/2022/06/30/outlook-clone.html; this is an adapted extract from the book). So it's not super urgent for me, but I would prefer to use Microsoft.Maui.Graphics for this sample.

  2. changed the title [-][Bug] Blend modes unimplemented on all platforms[/-] [+][Bug] Blend modes unimplemented on Android and Windows[/+] on Feb 1, 2023
  3. changed the title [-][Bug] Blend modes unimplemented on Android and Windows[/-] [+][Bug] Microsoft.Maui.Graphics Blend modes unimplemented on Android and Windows[/+] on May 5, 2023
  4. ghost added
    i/great-reporter
    on May 5, 2023
  5. added
    area-drawing
    on Jun 20, 2023
  6. RiversJohn commented on Jul 6, 2023

    @RiversJohn

    Err this means we can't actually blend at all using Microsoft.Maui.Graphics?

    That kind of eliminates the usability of the entire library if so does it not?

  7. added
    t/bug
    on Jul 31, 2023
  8. added
    s/triaged
    s/verified
    on Nov 23, 2023
  9. homeyf commented on Nov 23, 2023

    @homeyf

    Verified this issue with Visual Studio Enterprise 17.9.0 Preview 1.0. Can repro on windows platform with sample project.

  10. mgood7123 commented on Feb 9, 2024

    @mgood7123

    when will this be fixed ?

  11. mgood7123 commented on Feb 9, 2024

    @mgood7123

    also not even the Skia bindings implement BlendMode even though Skia supports it

  12. BlueRaja commented on Mar 22, 2024

    @BlueRaja

    It looks like it's already implemented but it's just commented out? For almost three years? Is this a joke!?

    I spent weeks porting from NGraphics to Maui Graphics, and now I need to spend several more weeks porting to SkiaSharp because the most basic barebones functionality has not been implemented?

    Does anyone know how to work around this with a handler maybe? I can't seem to figure out how to get an instance of the canvas (due, once again, to handlers being significantly less powerful than renderers).

  13. BlueRaja commented on Mar 23, 2024

    @BlueRaja

    Well I managed to patch together a massive hack that, at least, fulfills my needs. Here is the code for a control, OverwritingPlatformCanvas, which overwrites the destination pixels directly when drawing, rather than compositing using the alpha channel.

    1. In the non-Android-specific code, create a new control OverwritingPlatformCanvas

      public class OverwritingGraphicsView : GraphicsView { }
      
    2. In the Android Platform code, create the ViewHandler

      internal class OverwritingGraphicsViewHandler : ViewHandler<OverwritingGraphicsView, OverwritingPlatformGraphicsView>
      {
          private static PropertyMapper<OverwritingGraphicsView, OverwritingGraphicsViewHandler> Mapper = new(GraphicsViewHandler.ViewMapper);
          public OverwritingGraphicsViewHandler() : base(Mapper) { }
          public OverwritingGraphicsViewHandler([NotNull] IPropertyMapper mapper, CommandMapper commandMapper = null) : base(mapper, commandMapper) { }
      
          protected override OverwritingPlatformGraphicsView CreatePlatformView()
          {
              return new OverwritingPlatformGraphicsView(Context, VirtualView.Drawable);
          }
      }
      
    3. In the Android Platform code, create the PlatformGraphicsView, defined as class OverwritingPlatformGraphicsView : PlatformGraphicsView. The contents should be copy+pasted from Android/PlatformGraphicsView.cs. Then

      • Replace all instances of PlatformCanvas with OverwritingPlatformCanvas
      • Add this to all the constructors:
        SetLayerType(LayerType.Software, null); // ADDED DUE TO https://stackoverflow.com/q/18387814/238419
        

      The reason this is necessary is that the _canvas is marked as private instead of protected, so there is no way to override it from the child class πŸ€¦β€β™‚οΈ

      Using the contents of that file as of March 23, 2024, the full file looks like this:

       // Entire class was copy+pasted from
       // https://github.com/dotnet/maui/blob/4c23d90730e474c8f9b8b0f1eefb8f58e87b7f00/src/Graphics/src/Graphics/Platforms/Android/PlatformGraphicsView.cs#L11
       // except that PlatformCanvas was replaced with OverwritingPlatformCanvas
       internal class OverwritingPlatformGraphicsView : PlatformGraphicsView
       {
           private int _width, _height;
           private readonly OverwritingPlatformCanvas _canvas;
           private readonly ScalingCanvas _scalingCanvas;
           private IDrawable _drawable;
           private readonly float _scale = 1;
           private Color _backgroundColor;
      
           public OverwritingPlatformGraphicsView(Context context, IAttributeSet attrs, IDrawable drawable = null) : base(context, attrs)
           {
               _scale = Resources.DisplayMetrics.Density;
               _canvas = new OverwritingPlatformCanvas(context);
               _scalingCanvas = new ScalingCanvas(_canvas);
               Drawable = drawable;
               SetLayerType(LayerType.Software, null); // ADDED DUE TO https://stackoverflow.com/q/18387814/238419
           }
      
           public OverwritingPlatformGraphicsView(Context context, IDrawable drawable = null) : base(context)
           {
               _scale = Resources.DisplayMetrics.Density;
               _canvas = new OverwritingPlatformCanvas(context);
               _scalingCanvas = new ScalingCanvas(_canvas);
               Drawable = drawable;
               SetLayerType(LayerType.Software, null); // ADDED DUE TO https://stackoverflow.com/q/18387814/238419
           }
      
           public Color BackgroundColor
           {
               get => _backgroundColor;
               set
               {
                   _backgroundColor = value;
                   Invalidate();
               }
           }
      
           public IDrawable Drawable
           {
               get => _drawable;
               set
               {
                   _drawable = value;
                   Invalidate();
               }
           }
      
           public override void Draw(Canvas androidCanvas)
           {
               if (_drawable == null)
                   return;
      
               var dirtyRect = new RectF(0, 0, _width, _height);
      
               _canvas.Canvas = androidCanvas;
               if (_backgroundColor != null)
               {
                   _canvas.FillColor = _backgroundColor;
                   _canvas.FillRectangle(dirtyRect);
                   _canvas.FillColor = Colors.White;
               }
      
               _scalingCanvas.ResetState();
               _scalingCanvas.Scale(_scale, _scale);
               //Since we are using a scaling canvas, we need to scale the rectangle
               dirtyRect.Height /= _scale;
               dirtyRect.Width /= _scale;
               _drawable.Draw(_scalingCanvas, dirtyRect);
               _canvas.Canvas = null;
           }
      
           protected override void OnSizeChanged(int width, int height, int oldWidth, int oldHeight)
           {
               base.OnSizeChanged(width, height, oldWidth, oldHeight);
               _width = width;
               _height = height;
           }
       }
      
    4. In the Android Platform code, add the OverwritingPlatformCanvas

       internal class OverwritingPlatformCanvas : PlatformCanvas
       {
           public OverwritingPlatformCanvas(Context context) : base(context) { }
      
           private void UpdateFill()
           {
               CurrentState.FillPaintWithAlpha.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcOut));
               CurrentState.StrokePaintWithAlpha.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcOut));
               CurrentState.FontPaint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcOut));
           }
      
           public override void FillEllipse(float x, float y, float width, float height)
           {
               UpdateFill();
               base.FillEllipse(x, y, width, height);
           }
      
           public override void FillArc(float x, float y, float width, float height, float startAngle, float endAngle, bool clockwise)
           {
               UpdateFill();
               base.FillArc(x, y, width, height, startAngle, endAngle, clockwise);
           }
      
           public override void FillPath(PathF path, WindingMode windingMode)
           {
               UpdateFill();
               base.FillPath(path, windingMode);
           }
      
           public override void FillRectangle(float x, float y, float width, float height)
           {
               UpdateFill();
               base.FillRectangle(x, y, width, height);
           }
      
           public override void FillRoundedRectangle(float x, float y, float width, float height, float aCornerRadius)
           {
               UpdateFill();
               base.FillRoundedRectangle(x, y, width, height, aCornerRadius);
           }
      
           public override void DrawImage(IImage image, float x, float y, float width, float height)
           {
               UpdateFill();
               base.DrawImage(image, x, y, width, height);
           }
      
           public override void DrawString(string value, float x, float y, HorizontalAlignment horizAlignment)
           {
               UpdateFill();
               base.DrawString(value, x, y, horizAlignment);
           }
      
           public override void DrawString(string value, float x, float y, float width, float height, HorizontalAlignment horizAlignment,
               VerticalAlignment vertAlignment, TextFlow textFlow = TextFlow.ClipBounds, float lineSpacingAdjustment = 0)
           {
               UpdateFill();
               base.DrawString(value, x, y, width, height, horizAlignment, vertAlignment, textFlow, lineSpacingAdjustment);
           }
      
           public override void DrawText(IAttributedText value, float x, float y, float width, float height)
           {
               UpdateFill();
               base.DrawText(value, x, y, width, height);
           }
       }
      
    5. In your ConfigureMauiHandlers list in MauiProgram.cs, add the handler

      handlers.AddHandler<OverwritingGraphicsView, OverwritingGraphicsViewHandler>();
      
  14. jeremy-visionaid commented on Mar 25, 2024

    @jeremy-visionaid
    Contributor

    I also hit this issue and ended up just rewriting with SkiaSharp. FWIW, I think going with SkiaSharp is a better option than Microsoft.Maui.Graphics anyway. If you're doing custom drawing you probably want it to be pixel perfect on each platform, and I'm not sure I'd trust the wrapper approach of Microsoft.Maui.Graphics to accomplish that (as evidenced by this issue's existence).

  15. added this to the Backlog milestone on Jun 6, 2024
  16. JohnHDev commented on Feb 18, 2025

    @JohnHDev

    @jsuarezruiz Ive just found this after fighting with it on Android over the weekend, please update the docs to show it is only supported on iOS, or ask someone to fix it. It is unfair of MS to have documentation that shows this is possible when it isn't actually implemented. It wastes everyone's time.

  17. JohnHDev commented on Feb 19, 2025

    @JohnHDev

    @davidortinau I would suggest either updating the documentation to show that this is iOS only for now, or pull Maui.Graphics completely. Releasing half complete implementations does not help the community that wants to support Maui.

    Can you advise when / if this will ever be finished?

  18. davidbritch commented on Feb 19, 2025

    @davidbritch
    ContributorAuthor

    @JohnHDev It already says at the top of the doc that it's only implemented on iOS/MacCat.

  19. JohnHDev commented on Feb 19, 2025

    @JohnHDev

    @davidbritch You are quite right, in a big blue box that says Important. Missed that entirely, my mistake.
    Still begs the question, why does MS keep releasing incomplete features. It gets increasingly difficult to recommend .NET Maui.

  20. DarkNoir commented on Jan 16, 2026

    @DarkNoir

    Wow, YEARS and still no workey? MAUI drawing is still a joke. A bad joke. What gives?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-drawingi/great-reporters/triageds/verifiedt/bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions