Skip to content

Commit 74ce6d4

Browse files
authoredOct 30, 2023
PlatformImage.FromStream can be used on Windows (#1837)
* Use PlatformImage on Windows * PlatformImage can be used on WIndows.
1 parent 9875686 commit 74ce6d4

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
 

‎docs/user-interface/graphics/draw.md

+82
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ Images can be drawn on an <xref:Microsoft.Maui.Graphics.ICanvas> using the <xref
281281

282282
The following example shows how to load an image and draw it to the canvas:
283283

284+
::: moniker range="=net-maui-7.0"
285+
284286
```csharp
285287
using System.Reflection;
286288
using IImage = Microsoft.Maui.Graphics.IImage;
@@ -308,6 +310,30 @@ if (image != null)
308310
}
309311
```
310312

313+
::: moniker-end
314+
315+
::: moniker range=">=net-maui-8.0"
316+
317+
```csharp
318+
using System.Reflection;
319+
using IImage = Microsoft.Maui.Graphics.IImage;
320+
using Microsoft.Maui.Graphics.Platform;
321+
322+
IImage image;
323+
Assembly assembly = GetType().GetTypeInfo().Assembly;
324+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
325+
{
326+
image = PlatformImage.FromStream(stream);
327+
}
328+
329+
if (image != null)
330+
{
331+
canvas.DrawImage(image, 10, 10, image.Width, image.Height);
332+
}
333+
```
334+
335+
::: moniker-end
336+
311337
In this example, an image is retrieved from the assembly and loaded as a stream. It's then drawn at actual size at (10,10):
312338

313339
:::image type="content" source="media/draw/image.png" alt-text="Screenshot of an image.":::
@@ -524,6 +550,8 @@ Graphical objects that are drawn to an <xref:Microsoft.Maui.Graphics.ICanvas> ca
524550

525551
The following example shows how to use the <xref:Microsoft.Maui.Graphics.ICanvas.ClipPath%2A> method to clip an image:
526552

553+
::: moniker range="=net-maui-7.0"
554+
527555
```csharp
528556
using System.Reflection;
529557
using IImage = Microsoft.Maui.Graphics.IImage;
@@ -554,6 +582,33 @@ if (image != null)
554582
}
555583
```
556584

585+
::: moniker-end
586+
587+
::: moniker range=">=net-maui-8.0"
588+
589+
```csharp
590+
using System.Reflection;
591+
using IImage = Microsoft.Maui.Graphics.IImage;
592+
using Microsoft.Maui.Graphics.Platform;
593+
594+
IImage image;
595+
Assembly assembly = GetType().GetTypeInfo().Assembly;
596+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
597+
{
598+
image = PlatformImage.FromStream(stream);
599+
}
600+
601+
if (image != null)
602+
{
603+
PathF path = new PathF();
604+
path.AppendCircle(100, 90, 80);
605+
canvas.ClipPath(path); // Must be called before DrawImage
606+
canvas.DrawImage(image, 10, 10, image.Width, image.Height);
607+
}
608+
```
609+
610+
::: moniker-end
611+
557612
In this example, the image is clipped using a <xref:Microsoft.Maui.Graphics.PathF> 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:
558613

559614
:::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 <xref:Microsoft.Maui.Graphics.Path
563618
564619
The following example shows how to use the <xref:Microsoft.Maui.Graphics.ICanvas.SubtractFromClip%2A> method to clip an image:
565620

621+
::: moniker range="=net-maui-7.0"
622+
566623
```csharp
567624
using System.Reflection;
568625
using IImage = Microsoft.Maui.Graphics.IImage;
@@ -591,6 +648,31 @@ if (image != null)
591648
}
592649
```
593650

651+
::: moniker-end
652+
653+
::: moniker range=">=net-maui-8.0"
654+
655+
```csharp
656+
using System.Reflection;
657+
using IImage = Microsoft.Maui.Graphics.IImage;
658+
using Microsoft.Maui.Graphics.Platform;
659+
660+
IImage image;
661+
Assembly assembly = GetType().GetTypeInfo().Assembly;
662+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
663+
{
664+
image = PlatformImage.FromStream(stream);
665+
}
666+
667+
if (image != null)
668+
{
669+
canvas.SubtractFromClip(60, 60, 90, 90);
670+
canvas.DrawImage(image, 10, 10, image.Width, image.Height);
671+
}
672+
```
673+
674+
::: moniker-end
675+
594676
In this example, the area defined by the rectangle that's specified by the arguments supplied to the <xref:Microsoft.Maui.Graphics.ICanvas.SubtractFromClip%2A> method is clipped from the image. The result is that only the parts of the image outside the rectangle are visible:
595677

596678
:::image type="content" source="media/draw/subtractfromclip.png" alt-text="Screenshot of an image that's been clipped with the SubtractFromClip method.":::

‎docs/user-interface/graphics/images.md

+111
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Image loading functionality is provided by the <xref:Microsoft.Maui.Graphics.Pla
2626

2727
The following example shows how to load an image:
2828

29+
::: moniker range="=net-maui-7.0"
30+
2931
```csharp
3032
#if IOS || ANDROID || MACCATALYST
3133
using Microsoft.Maui.Graphics.Platform;
@@ -53,6 +55,30 @@ if (image != null)
5355
}
5456
```
5557

58+
::: moniker-end
59+
60+
::: moniker range=">=net-maui-8.0"
61+
62+
```csharp
63+
using Microsoft.Maui.Graphics.Platform;
64+
using System.Reflection;
65+
using IImage = Microsoft.Maui.Graphics.IImage;
66+
67+
IImage image;
68+
Assembly assembly = GetType().GetTypeInfo().Assembly;
69+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
70+
{
71+
image = PlatformImage.FromStream(stream);
72+
}
73+
74+
if (image != null)
75+
{
76+
canvas.DrawImage(image, 10, 10, image.Width, image.Height);
77+
}
78+
```
79+
80+
::: moniker-end
81+
5682
In this example, the image is retrieved from the assembly, loaded as a stream, and displayed.
5783

5884
> [!IMPORTANT]
@@ -73,6 +99,8 @@ The <xref:Microsoft.Maui.Graphics.ResizeMode> enumeration defines the following
7399

74100
The following example shows how to resize an image:
75101

102+
::: moniker range="=net-maui-7.0"
103+
76104
```csharp
77105
#if IOS || ANDROID || MACCATALYST
78106
using Microsoft.Maui.Graphics.Platform;
@@ -101,6 +129,31 @@ if (image != null)
101129
}
102130
```
103131

132+
::: moniker-end
133+
134+
::: moniker range=">=net-maui-8.0"
135+
136+
```csharp
137+
using Microsoft.Maui.Graphics.Platform;
138+
using System.Reflection;
139+
using IImage = Microsoft.Maui.Graphics.IImage;
140+
141+
IImage image;
142+
Assembly assembly = GetType().GetTypeInfo().Assembly;
143+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
144+
{
145+
image = PlatformImage.FromStream(stream);
146+
}
147+
148+
if (image != null)
149+
{
150+
IImage newImage = image.Resize(100, 60, ResizeMode.Stretch, true);
151+
canvas.DrawImage(newImage, 10, 10, newImage.Width, newImage.Height);
152+
}
153+
```
154+
155+
::: moniker-end
156+
104157
In this example, the image is retrieved from the assembly and loaded as a stream. The image is resized using the <xref:Microsoft.Maui.Graphics.IImage.Resize%2A> 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).
105158

106159
## Downsize an image
@@ -111,6 +164,8 @@ The <xref:Microsoft.Maui.Graphics.IImage.Downsize%2A> overloads also accept an o
111164

112165
The following example shows how to downsize an image:
113166

167+
::: moniker range="=net-maui-7.0"
168+
114169
```csharp
115170
#if IOS || ANDROID || MACCATALYST
116171
using Microsoft.Maui.Graphics.Platform;
@@ -139,6 +194,31 @@ if (image != null)
139194
}
140195
```
141196

197+
::: moniker-end
198+
199+
::: moniker range=">=net-maui-8.0"
200+
201+
```csharp
202+
using Microsoft.Maui.Graphics.Platform;
203+
using System.Reflection;
204+
using IImage = Microsoft.Maui.Graphics.IImage;
205+
206+
IImage image;
207+
Assembly assembly = GetType().GetTypeInfo().Assembly;
208+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
209+
{
210+
image = PlatformImage.FromStream(stream);
211+
}
212+
213+
if (image != null)
214+
{
215+
IImage newImage = image.Downsize(100, true);
216+
canvas.DrawImage(newImage, 10, 10, newImage.Width, newImage.Height);
217+
}
218+
```
219+
220+
::: moniker-end
221+
142222
In this example, the image is retrieved from the assembly and loaded as a stream. The image is downsized using the <xref:Microsoft.Maui.Graphics.IImage.Downsize%2A> 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).
143223

144224
## Save an image
@@ -150,6 +230,8 @@ You can save images using the <xref:Microsoft.Maui.Graphics.IImage.Save%2A> and
150230
151231
The following example shows how to save an image:
152232

233+
::: moniker range="=net-maui-7.0"
234+
153235
```csharp
154236
#if IOS || ANDROID || MACCATALYST
155237
using Microsoft.Maui.Graphics.Platform;
@@ -182,4 +264,33 @@ if (image != null)
182264
}
183265
```
184266

267+
::: moniker-end
268+
269+
::: moniker range=">=net-maui-8.0"
270+
271+
```csharp
272+
using Microsoft.Maui.Graphics.Platform;
273+
using System.Reflection;
274+
using IImage = Microsoft.Maui.Graphics.IImage;
275+
276+
IImage image;
277+
Assembly assembly = GetType().GetTypeInfo().Assembly;
278+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
279+
{
280+
image = PlatformImage.FromStream(stream);
281+
}
282+
283+
// Save image to a memory stream
284+
if (image != null)
285+
{
286+
IImage newImage = image.Downsize(150, true);
287+
using (MemoryStream memStream = new MemoryStream())
288+
{
289+
newImage.Save(memStream);
290+
}
291+
}
292+
```
293+
294+
::: moniker-end
295+
185296
In this example, the image is retrieved from the assembly and loaded as a stream. The image is downsized using the <xref:Microsoft.Maui.Graphics.IImage.Downsize%2A> 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.

‎docs/user-interface/graphics/paint.md

+31
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ To paint an object with an image, load the image and assign it to the <xref:Micr
7373
7474
The following example shows how to load an image and fill a rectangle with it:
7575

76+
::: moniker range="=net-maui-7.0"
77+
7678
```csharp
7779
using System.Reflection;
7880
using IImage = Microsoft.Maui.Graphics.IImage;
@@ -105,6 +107,35 @@ if (image != null)
105107
}
106108
```
107109

110+
::: moniker-end
111+
112+
::: moniker range=">=net-maui-8.0"
113+
114+
```csharp
115+
using System.Reflection;
116+
using IImage = Microsoft.Maui.Graphics.IImage;
117+
using Microsoft.Maui.Graphics.Platform;
118+
119+
IImage image;
120+
Assembly assembly = GetType().GetTypeInfo().Assembly;
121+
using (Stream stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
122+
{
123+
image = PlatformImage.FromStream(stream);
124+
}
125+
126+
if (image != null)
127+
{
128+
ImagePaint imagePaint = new ImagePaint
129+
{
130+
Image = image.Downsize(100)
131+
};
132+
canvas.SetFillPaint(imagePaint, RectF.Zero);
133+
canvas.FillRectangle(0, 0, 240, 300);
134+
}
135+
```
136+
137+
::: moniker-end
138+
108139
In this example, the image is retrieved from the assembly and loaded as a stream. The image is resized using the <xref:Microsoft.Maui.Graphics.IImage.Downsize%2a> 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).
109140

110141
The <xref:Microsoft.Maui.Graphics.ImagePaint.Image> property of the <xref:Microsoft.Maui.Graphics.ImagePaint> object is set to the downsized version of the image, and the <xref:Microsoft.Maui.Graphics.ImagePaint> object is set as the paint to fill an object with. A rectangle is then drawn that's filled with the paint:

‎docs/whats-new/dotnet-8.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The following behavior has changed from the previous release:
6767
- 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).
6868
- 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.
6969
- .NET MAUI Mac Catalyst apps are no longer limited to 50 menu items on the menu bar.
70+
- 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.
7071

7172
<!-- ## Performance
7273

0 commit comments

Comments
 (0)