Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<MauiFont Remove="Resources\Fonts\Dokdo-Regular.ttf" />
<EmbeddedResource Include="Resources\Fonts\Dokdo-Regular.ttf" />
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
<EmbeddedResource Include="Resources\Images\royals.png" />
</ItemGroup>

<ItemGroup>
Expand Down
88 changes: 88 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue21886.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Reflection;
using Microsoft.Maui.Graphics.Platform;
using IImage = Microsoft.Maui.Graphics.IImage;

namespace Controls.TestCases.HostApp.Issues;

[Issue(IssueTracker.Github, 21886, "The original image remains undisposed even after setting disposeOriginal to true in the Resize and Downsize methods", PlatformAffected.Android | PlatformAffected.iOS)]
public class Issue21886 : ContentPage
{
Label _originalImageStatusLabel;

public Issue21886()
{
_originalImageStatusLabel = new Label
{
AutomationId = "OriginalImageStatusLabel",
Text = "Status of Original Image Disposal"
};

VerticalStackLayout stackLayout = new VerticalStackLayout
{
Children =
{
CreateButton("Resize", OnResize),
CreateButton("DownSize", OnDownSize),
_originalImageStatusLabel,
}
};

Content = new ScrollView { Content = stackLayout };
}

Button CreateButton(string text, EventHandler handler)
{
Button button = new Button
{
AutomationId = $"Issue21886{text}Btn",
Text = text,
HorizontalOptions = LayoutOptions.Fill
};

button.Clicked += handler;
return button;
}

async Task<IImage> LoadImageAsync()
{
var assembly = GetType().GetTypeInfo().Assembly;
using var stream = assembly.GetManifestResourceStream("Controls.TestCases.HostApp.Resources.Images.royals.png");
return await Task.FromResult(PlatformImage.FromStream(stream));
}

async void OnResize(object sender, EventArgs e)
{
var image = await LoadImageAsync();
var res = image.Resize(10, 10, ResizeMode.Fit, true);

UpdateStatusLabels(res, image, "Resize");
}

async void OnDownSize(object sender, EventArgs e)
{
var image = await LoadImageAsync();
var res = image.Downsize(10, 10, true);

UpdateStatusLabels(res, image, "Downsize");
}

void UpdateStatusLabels(IImage resultImage, IImage originalImage, string operation)
{
_originalImageStatusLabel.Text = TryAccessImage(originalImage)
? "Success"
: originalImage.Width == 0 && originalImage.Height == 0 ? "Success" : "Failure";
}

bool TryAccessImage(IImage image)
{
try
{
var _ = image.Width;
return false;
}
catch (ObjectDisposedException)
{
return true;
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#if TEST_FAILS_ON_WINDOWS // Issue Link - https://github.com/dotnet/maui/issues/16767
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue21886 : _IssuesUITest
{
public Issue21886(TestDevice device) : base(device)
{
}

public override string Issue => "The original image remains undisposed even after setting disposeOriginal to true in the Resize and Downsize methods";

[Test]
[Category(UITestCategories.GraphicsView)]
public void VerifyOriginalImageBeingDisposed()
{
App.WaitForElement("OriginalImageStatusLabel");
App.Tap("Issue21886ResizeBtn");

var resizeLabelText = App.FindElement("OriginalImageStatusLabel").GetText();
Assert.That(resizeLabelText, Is.EqualTo("Success"));

App.Tap("Issue21886DownSizeBtn");

var downsizeLabelText = App.FindElement("OriginalImageStatusLabel").GetText();
Assert.That(downsizeLabelText, Is.EqualTo("Success"));
}
}
#endif
11 changes: 9 additions & 2 deletions src/Graphics/src/Graphics/Platforms/Android/PlatformImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public PlatformImage(Bitmap bitmap)
_bitmap = bitmap;
}

public float Width => _bitmap.Width;
public float Width => _bitmap?.Width ?? 0;

public float Height => _bitmap.Height;
public float Height => _bitmap?.Height ?? 0;

public IImage Downsize(float maxWidthOrHeight, bool disposeOriginal = false)
{
Expand Down Expand Up @@ -83,6 +83,13 @@ public IImage Resize(float width, float height, ResizeMode resizeMode = ResizeMo
}

context.Canvas.DrawImage(this, x, y, w, h);

if (disposeOriginal)
{
_bitmap.Recycle();
_bitmap.Dispose();
}

return context.Image;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Graphics/src/Graphics/Platforms/iOS/PlatformImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public IImage Resize(float width, float height, ResizeMode resizeMode = ResizeMo
}

context.Canvas.DrawImage(this, x, y, w, h);

if (disposeOriginal)
{
_image.Dispose();
}

return context.Image;
}
}
Expand Down
Loading