Skip to content

Commit 5442a5e

Browse files
[Windows] Implemented the Resize and Downsize functions in the W2DImage class (#29138)
* Fix for GraphicsView ImagePaint * update for the graphics view imagePaint * Added testcase * Implementation for Downsize * Revert "Added testcase" This reverts commit ef22fa8. * Added test case * Added CreateResizedMethod * Implemented Resize method * Fix modification * Added testcase * Revert "Added test case" This reverts commit a79de0d. * Updated the changes * Update changes * Updated the testcase * Commited the changes * Updating code * Update TestCase * Code Update * Modify TestCase * TestCase Updation * Code update * Adding Snapshots * Added snapshots * Revert "Added snapshots" This reverts commit cac6f45. * Refactor code
1 parent 56633e3 commit 5442a5e

File tree

16 files changed

+347
-23
lines changed

16 files changed

+347
-23
lines changed
220 KB
Loading
221 KB
Loading
221 KB
Loading
126 KB
Loading

src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<MauiFont Include="Resources\Fonts\**" />
6767
<MauiFont Remove="Resources\Fonts\Dokdo-Regular.ttf" />
6868
<EmbeddedResource Include="Resources\Fonts\Dokdo-Regular.ttf" />
69+
<EmbeddedResource Include="Resources\Images\royals.png" />
6970
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
7071
</ItemGroup>
7172

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Reflection;
2+
using Maui.Controls.Sample.Issues;
3+
using Microsoft.Maui.Graphics.Platform;
4+
using IImage = Microsoft.Maui.Graphics.IImage;
5+
6+
namespace Controls.TestCases.HostApp.Issues;
7+
8+
[Issue(IssueTracker.Github, 28725, "Downsize function in W2DImage class", PlatformAffected.UWP)]
9+
10+
public class Issue16767_DownSize : TestContentPage
11+
{
12+
GraphicsView downSizeGraphicsView;
13+
Issue16767_DownsizeDrawable downSizeDrawable;
14+
15+
protected override void Init()
16+
{
17+
var rootLayout = new VerticalStackLayout();
18+
19+
downSizeGraphicsView = new GraphicsView()
20+
{
21+
BackgroundColor = Colors.Red,
22+
HeightRequest = 400,
23+
WidthRequest = 400
24+
};
25+
26+
Label descriptionLabel = new Label()
27+
{
28+
Text = "The test should pass if the image is displayed and resized correctly. If the image is not displayed or not downsized correctly, the test has failed.",
29+
HorizontalOptions = LayoutOptions.Center,
30+
VerticalOptions = LayoutOptions.Center,
31+
AutomationId = "descriptionLabel"
32+
};
33+
34+
downSizeDrawable = new Issue16767_DownsizeDrawable();
35+
downSizeGraphicsView.Drawable = downSizeDrawable;
36+
rootLayout.Add(downSizeGraphicsView);
37+
38+
rootLayout.Add(descriptionLabel);
39+
Content = rootLayout;
40+
}
41+
}
42+
43+
public class Issue16767_DownsizeDrawable : IDrawable
44+
{
45+
public void Draw(ICanvas canvas, RectF dirtyRect)
46+
{
47+
IImage image;
48+
var assembly = GetType().GetTypeInfo().Assembly;
49+
using (var stream = assembly.GetManifestResourceStream("Controls.TestCases.HostApp.Resources.Images.royals.png"))
50+
{
51+
image = PlatformImage.FromStream(stream);
52+
}
53+
if (image is not null)
54+
{
55+
float spacing = 20;
56+
float currentY = 0;
57+
58+
canvas.FontColor = Colors.Black;
59+
canvas.FontSize = 16;
60+
61+
// Label before first image
62+
canvas.DrawString("Downsize (100, 200)", 0, currentY, dirtyRect.Width, 30, HorizontalAlignment.Left, VerticalAlignment.Top);
63+
currentY += 30;
64+
65+
var downsized1 = image.Downsize(100, 200);
66+
canvas.SetFillImage(downsized1);
67+
canvas.FillRectangle(0, currentY, 240, downsized1.Height);
68+
currentY += downsized1.Height + spacing;
69+
70+
// Label before second image
71+
canvas.DrawString("Downsize (100)", 0, currentY, dirtyRect.Width, 30, HorizontalAlignment.Left, VerticalAlignment.Top);
72+
currentY += 30;
73+
74+
var downsized2 = image.Downsize(100);
75+
canvas.SetFillImage(downsized2);
76+
canvas.FillRectangle(0, currentY, 240, downsized2.Height);
77+
}
78+
}
79+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System.Reflection;
2+
using Maui.Controls.Sample.Issues;
3+
using Microsoft.Maui.Graphics.Platform;
4+
using IImage = Microsoft.Maui.Graphics.IImage;
5+
6+
namespace Controls.TestCases.HostApp.Issues;
7+
8+
[Issue(IssueTracker.Github, 16767, "Resize function in W2DImage class", PlatformAffected.UWP)]
9+
10+
public class Issue16767_Resize : TestContentPage
11+
{
12+
GraphicsView ReSizeGraphicsView;
13+
Issue16767_ResizeDrawable resizeDrawable;
14+
15+
RadioButton resizeModeFit;
16+
RadioButton resizeModeBleed;
17+
RadioButton resizeModeStretch;
18+
protected override void Init()
19+
{
20+
21+
var rootLayout = new VerticalStackLayout();
22+
23+
ReSizeGraphicsView = new GraphicsView()
24+
{
25+
HeightRequest = 300,
26+
WidthRequest = 300
27+
};
28+
resizeDrawable = new Issue16767_ResizeDrawable();
29+
ReSizeGraphicsView.Drawable = resizeDrawable;
30+
rootLayout.Add(CreateResizeModeSelection());
31+
rootLayout.Add(ReSizeGraphicsView);
32+
Content = rootLayout;
33+
}
34+
35+
HorizontalStackLayout CreateResizeModeSelection()
36+
{
37+
var horizontalStackLayout = new HorizontalStackLayout()
38+
{
39+
Spacing = 10,
40+
};
41+
42+
resizeModeFit = CreateRadioButton("Fit", "ResizeModeSelection", true, "ResizeModeFit");
43+
resizeModeBleed = CreateRadioButton("Bleed", "ResizeModeSelection", false, "ResizeModeBleed");
44+
resizeModeStretch = CreateRadioButton("Stretch", "ResizeModeSelection", false, "ResizeModeStretch");
45+
46+
resizeModeFit.CheckedChanged += OnResizeModeChanged;
47+
resizeModeBleed.CheckedChanged += OnResizeModeChanged;
48+
resizeModeStretch.CheckedChanged += OnResizeModeChanged;
49+
50+
horizontalStackLayout.Children.Add(resizeModeFit);
51+
horizontalStackLayout.Children.Add(resizeModeBleed);
52+
horizontalStackLayout.Children.Add(resizeModeStretch);
53+
54+
return horizontalStackLayout;
55+
}
56+
57+
RadioButton CreateRadioButton(string text, string groupName, bool isChecked, string automationID)
58+
{
59+
return new RadioButton
60+
{
61+
Content = text,
62+
GroupName = groupName,
63+
IsChecked = isChecked,
64+
FontSize = 11,
65+
AutomationId = automationID
66+
};
67+
}
68+
69+
void OnResizeModeChanged(object sender, CheckedChangedEventArgs e)
70+
{
71+
if (resizeModeFit.IsChecked)
72+
{
73+
resizeDrawable.SetResizeMode(ResizeMode.Fit);
74+
ReSizeGraphicsView.Invalidate();
75+
}
76+
else if (resizeModeBleed.IsChecked)
77+
{
78+
resizeDrawable.SetResizeMode(ResizeMode.Bleed);
79+
ReSizeGraphicsView.Invalidate();
80+
}
81+
else if (resizeModeStretch.IsChecked)
82+
{
83+
resizeDrawable.SetResizeMode(ResizeMode.Stretch);
84+
ReSizeGraphicsView.Invalidate();
85+
}
86+
}
87+
}
88+
89+
public class Issue16767_ResizeDrawable : IDrawable
90+
{
91+
ResizeMode _resizeMode;
92+
93+
internal void SetResizeMode(ResizeMode resizeMode)
94+
{
95+
_resizeMode = resizeMode;
96+
}
97+
98+
public void Draw(ICanvas canvas, RectF dirtyRect)
99+
{
100+
IImage image;
101+
var assembly = GetType().GetTypeInfo().Assembly;
102+
using (var stream = assembly.GetManifestResourceStream("Controls.TestCases.HostApp.Resources.Images.royals.png"))
103+
{
104+
image = PlatformImage.FromStream(stream);
105+
}
106+
107+
if (image is not null)
108+
{
109+
var resizedImage = image.Resize(100, 200, _resizeMode);
110+
canvas.SetFillImage(resizedImage);
111+
canvas.FillRectangle(0, 0, 200, resizedImage.Height);
112+
}
113+
}
114+
}
84 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if TEST_FAILS_ON_CATALYST && TEST_FAILS_ON_IOS //NullReferenceException throws on iOS and mac Issue link - https://github.com/dotnet/maui/issues/19642
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
public class Issue16767_Downsize : _IssuesUITest
8+
{
9+
public Issue16767_Downsize(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "Downsize function in W2DImage class";
14+
15+
[Test]
16+
[Category(UITestCategories.GraphicsView)]
17+
public void ImagePaintWithDownsize()
18+
{
19+
App.WaitForElement("descriptionLabel");
20+
VerifyScreenshot();
21+
}
22+
}
23+
#endif
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#if TEST_FAILS_ON_CATALYST && TEST_FAILS_ON_IOS //NullReferenceException throws on iOS and mac Issue link - https://github.com/dotnet/maui/issues/19642
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
public class Issue16767_Resize : _IssuesUITest
8+
{
9+
public Issue16767_Resize(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "Resize function in W2DImage class";
14+
15+
[Test]
16+
[Category(UITestCategories.GraphicsView)]
17+
public void ImagePaintWithResizeModeFit()
18+
{
19+
App.WaitForElement("ResizeModeFit");
20+
VerifyScreenshot();
21+
}
22+
23+
[Test]
24+
[Category(UITestCategories.GraphicsView)]
25+
public void ImagePaintWithResizeModeBleed()
26+
{
27+
App.WaitForElement("ResizeModeBleed");
28+
App.Tap("ResizeModeBleed");
29+
VerifyScreenshot();
30+
}
31+
32+
[Test]
33+
[Category(UITestCategories.GraphicsView)]
34+
public void ImagePaintWithResizeModeStretch()
35+
{
36+
App.WaitForElement("ResizeModeStretch");
37+
App.Tap("ResizeModeStretch");
38+
VerifyScreenshot();
39+
}
40+
}
41+
#endif

0 commit comments

Comments
 (0)