You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Microsoft.Maui.Graphics Blend modes unimplemented on Android and WindowsΒ #14945
area-drawingShapes, Borders, Shadows, Graphics, BoxView, custom drawingi/great-reporterThis issue is opened by an author who is known to open high-quality issuess/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working
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.
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
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).
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.
In the non-Android-specific code, create a new control OverwritingPlatformCanvas
public class OverwritingGraphicsView : GraphicsView { }
In the Android Platform code, create the ViewHandler
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;
}
}
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);
}
}
In your ConfigureMauiHandlers list in MauiProgram.cs, add the handler
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).
@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.
@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?
@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.
area-drawingShapes, Borders, Shadows, Graphics, BoxView, custom drawingi/great-reporterThis issue is opened by an author who is known to open high-quality issuess/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working
Activity
matt-goldman commentedon Nov 20, 2022
@jsuarezruiz looks like this is not getting any attention. I've been waiting for this for a while but wondering at this stage:
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.Graphicsfor this sample.[-][Bug] Blend modes unimplemented on all platforms[/-][+][Bug] Blend modes unimplemented on Android and Windows[/+][-][Bug] Blend modes unimplemented on Android and Windows[/-][+][Bug] Microsoft.Maui.Graphics Blend modes unimplemented on Android and Windows[/+]RiversJohn commentedon Jul 6, 2023
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?
homeyf commentedon Nov 23, 2023
Verified this issue with Visual Studio Enterprise 17.9.0 Preview 1.0. Can repro on windows platform with sample project.
mgood7123 commentedon Feb 9, 2024
when will this be fixed ?
mgood7123 commentedon Feb 9, 2024
also not even the Skia bindings implement BlendMode even though Skia supports it
BlueRaja commentedon Mar 22, 2024
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).
BlueRaja commentedon Mar 23, 2024
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.In the non-Android-specific code, create a new control
OverwritingPlatformCanvasIn the Android Platform code, create the ViewHandler
In the Android Platform code, create the PlatformGraphicsView, defined as
class OverwritingPlatformGraphicsView : PlatformGraphicsView. The contents should be copy+pasted from Android/PlatformGraphicsView.cs. ThenPlatformCanvaswithOverwritingPlatformCanvasThe reason this is necessary is that the
_canvasis marked asprivateinstead ofprotected, 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:
In the Android Platform code, add the OverwritingPlatformCanvas
In your
ConfigureMauiHandlerslist inMauiProgram.cs, add the handlerjeremy-visionaid commentedon Mar 25, 2024
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).
JohnHDev commentedon Feb 18, 2025
@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.
JohnHDev commentedon Feb 19, 2025
@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?
davidbritch commentedon Feb 19, 2025
@JohnHDev It already says at the top of the doc that it's only implemented on iOS/MacCat.
JohnHDev commentedon Feb 19, 2025
@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.
DarkNoir commentedon Jan 16, 2026
Wow, YEARS and still no workey? MAUI drawing is still a joke. A bad joke. What gives?