diff --git a/docs/user-interface/graphics/draw.md b/docs/user-interface/graphics/draw.md index e9ac76d6e1..562bc0375c 100644 --- a/docs/user-interface/graphics/draw.md +++ b/docs/user-interface/graphics/draw.md @@ -281,6 +281,8 @@ Images can be drawn on an using the ca The following example shows how to use the method to clip an image: +::: moniker range="=net-maui-7.0" + ```csharp using System.Reflection; using IImage = Microsoft.Maui.Graphics.IImage; @@ -554,6 +582,33 @@ if (image != null) } ``` +::: moniker-end + +::: moniker range=">=net-maui-8.0" + +```csharp +using System.Reflection; +using IImage = Microsoft.Maui.Graphics.IImage; +using Microsoft.Maui.Graphics.Platform; + +IImage image; +Assembly assembly = GetType().GetTypeInfo().Assembly; +using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png")) +{ + image = PlatformImage.FromStream(stream); +} + +if (image != null) +{ + PathF path = new PathF(); + path.AppendCircle(100, 90, 80); + canvas.ClipPath(path); // Must be called before DrawImage + canvas.DrawImage(image, 10, 10, image.Width, image.Height); +} +``` + +::: moniker-end + In this example, the image is clipped using a object that defines a circle that's centered at (100,90) with a radius of 80. The result is that only the part of the image within the circle is visible: :::image type="content" source="media/draw/clippath.png" alt-text="Screenshot of an image that's been clipped with the ClipPath method."::: @@ -563,6 +618,8 @@ In this example, the image is clipped using a method to clip an image: +::: moniker range="=net-maui-7.0" + ```csharp using System.Reflection; using IImage = Microsoft.Maui.Graphics.IImage; @@ -591,6 +648,31 @@ if (image != null) } ``` +::: moniker-end + +::: moniker range=">=net-maui-8.0" + +```csharp +using System.Reflection; +using IImage = Microsoft.Maui.Graphics.IImage; +using Microsoft.Maui.Graphics.Platform; + +IImage image; +Assembly assembly = GetType().GetTypeInfo().Assembly; +using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png")) +{ + image = PlatformImage.FromStream(stream); +} + +if (image != null) +{ + canvas.SubtractFromClip(60, 60, 90, 90); + canvas.DrawImage(image, 10, 10, image.Width, image.Height); +} +``` + +::: moniker-end + In this example, the area defined by the rectangle that's specified by the arguments supplied to the method is clipped from the image. The result is that only the parts of the image outside the rectangle are visible: :::image type="content" source="media/draw/subtractfromclip.png" alt-text="Screenshot of an image that's been clipped with the SubtractFromClip method."::: diff --git a/docs/user-interface/graphics/images.md b/docs/user-interface/graphics/images.md index d345038ac9..18bd64e86e 100644 --- a/docs/user-interface/graphics/images.md +++ b/docs/user-interface/graphics/images.md @@ -26,6 +26,8 @@ Image loading functionality is provided by the [!IMPORTANT] @@ -73,6 +99,8 @@ The enumeration defines the following The following example shows how to resize an image: +::: moniker range="=net-maui-7.0" + ```csharp #if IOS || ANDROID || MACCATALYST using Microsoft.Maui.Graphics.Platform; @@ -101,6 +129,31 @@ if (image != null) } ``` +::: moniker-end + +::: moniker range=">=net-maui-8.0" + +```csharp +using Microsoft.Maui.Graphics.Platform; +using System.Reflection; +using IImage = Microsoft.Maui.Graphics.IImage; + +IImage image; +Assembly assembly = GetType().GetTypeInfo().Assembly; +using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png")) +{ + image = PlatformImage.FromStream(stream); +} + +if (image != null) +{ + IImage newImage = image.Resize(100, 60, ResizeMode.Stretch, true); + canvas.DrawImage(newImage, 10, 10, newImage.Width, newImage.Height); +} +``` + +::: moniker-end + In this example, the image is retrieved from the assembly and loaded as a stream. The image is resized using the method, with its arguments specifying the new size, and that it should be stretched to fill the available space. In addition, the source image is disposed. The resized image is then drawn at actual size at (10,10). ## Downsize an image @@ -111,6 +164,8 @@ The overloads also accept an o The following example shows how to downsize an image: +::: moniker range="=net-maui-7.0" + ```csharp #if IOS || ANDROID || MACCATALYST using Microsoft.Maui.Graphics.Platform; @@ -139,6 +194,31 @@ if (image != null) } ``` +::: moniker-end + +::: moniker range=">=net-maui-8.0" + +```csharp +using Microsoft.Maui.Graphics.Platform; +using System.Reflection; +using IImage = Microsoft.Maui.Graphics.IImage; + +IImage image; +Assembly assembly = GetType().GetTypeInfo().Assembly; +using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png")) +{ + image = PlatformImage.FromStream(stream); +} + +if (image != null) +{ + IImage newImage = image.Downsize(100, true); + canvas.DrawImage(newImage, 10, 10, newImage.Width, newImage.Height); +} +``` + +::: moniker-end + In this example, the image is retrieved from the assembly and loaded as a stream. The image is downsized using the method, with the argument specifying that its largest dimension should be set to 100 pixels. In addition, the source image is disposed. The downsized image is then drawn at actual size at (10,10). ## Save an image @@ -150,6 +230,8 @@ You can save images using the and The following example shows how to save an image: +::: moniker range="=net-maui-7.0" + ```csharp #if IOS || ANDROID || MACCATALYST using Microsoft.Maui.Graphics.Platform; @@ -182,4 +264,33 @@ if (image != null) } ``` +::: moniker-end + +::: moniker range=">=net-maui-8.0" + +```csharp +using Microsoft.Maui.Graphics.Platform; +using System.Reflection; +using IImage = Microsoft.Maui.Graphics.IImage; + +IImage image; +Assembly assembly = GetType().GetTypeInfo().Assembly; +using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png")) +{ + image = PlatformImage.FromStream(stream); +} + +// Save image to a memory stream +if (image != null) +{ + IImage newImage = image.Downsize(150, true); + using (MemoryStream memStream = new MemoryStream()) + { + newImage.Save(memStream); + } +} +``` + +::: moniker-end + In this example, the image is retrieved from the assembly and loaded as a stream. The image is downsized using the method, with the argument specifying that its largest dimension should be set to 150 pixels. In addition, the source image is disposed. The downsized image is then saved to a stream. diff --git a/docs/user-interface/graphics/paint.md b/docs/user-interface/graphics/paint.md index 2c6c3c824d..eebd0d58dd 100644 --- a/docs/user-interface/graphics/paint.md +++ b/docs/user-interface/graphics/paint.md @@ -73,6 +73,8 @@ To paint an object with an image, load the image and assign it to the method, with the argument specifying that its largest dimension should be set to 100 pixels. For more information about downsizing an image, see [Downsize an image](~/user-interface/graphics/images.md#downsize-an-image). The property of the object is set to the downsized version of the image, and the object is set as the paint to fill an object with. A rectangle is then drawn that's filled with the paint: diff --git a/docs/whats-new/dotnet-8.md b/docs/whats-new/dotnet-8.md index 49f599c09f..5f4a7a869f 100644 --- a/docs/whats-new/dotnet-8.md +++ b/docs/whats-new/dotnet-8.md @@ -67,6 +67,7 @@ The following behavior has changed from the previous release: - How the color of a tab is set in a Shell app has changed on some platforms. For more information, see [Tab appearance](~/fundamentals/shell/tabs.md#tab-appearance). - It's not required to specify a value for the `$(ApplicationIdGuid)` build property in your app's project file. This is because .NET MAUI Windows apps no longer require a GUID as an app ID, and instead use the value of the `$(ApplicationId)` build property as the app ID. Therefore, the same reverse domain format app ID is now used across all platforms, such as com.mycompany.myapp. - .NET MAUI Mac Catalyst apps are no longer limited to 50 menu items on the menu bar. +- The `PlatformImage.FromStream` method, in the `Microsoft.Maui.Graphics` namespace, can now be used to load images on Windows instead of having to use the `W2DImageLoadingService` class.