简体   繁体   中英

Bitmap object needed in MAUI.NET?

I need to store a bitmap object in my MAUI.NET application.

To be clear - in my definition: bitmap is a representation of the image by the 2 dimensional array of pixels, that have at least R,G and B value.

In .NET 4.7 it wasn't already such an object, but there was a NuGet System.Drawings.Common that allowed me to use such an object.

How to handle such a situation in MAUI.NET?

If I understand your means correctly, you can paint graphical objects in the Microsoft.Maui.Graphics namespace.

First ,Images can be drawn on an ICanvas using the DrawImage method, which requires an IImage argument, and x , y , width , and height arguments, of type float.

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

  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.DrawImage(image, 10, 10, image.Width, image.Height);
}

For more information, you can check: Draw an image .

Second , you can also paint an image.The ImagePaint class, that's derived from the Paint class, is used to paint a graphical object with an image.

The ImagePaint class defines an Image property, of type IImage , which represents the image to paint. The class also has an IsTransparent property that returns false.

To paint an object with an image, load the image and assign it to the Image property of the ImagePaint object.

The following example shows how to load an image and fill a rectangle with it:

using Microsoft.Maui.Graphics.Platform;
...

IImage image;
var assembly = GetType().GetTypeInfo().Assembly;
using (var stream = assembly.GetManifestResourceStream("GraphicsViewDemos.Resources.Images.dotnet_bot.png"))
{
    image = PlatformImage.FromStream(stream);
}

if (image != null)
{
    ImagePaint imagePaint = new ImagePaint
    {
        Image = image.Downsize(100)
    };
    canvas.SetFillPaint(imagePaint, RectF.Zero);
    canvas.FillRectangle(0, 0, 240, 300);
}

For more details, you can check: Paint graphical objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM