简体   繁体   English

MAUI.NET 中需要 Bitmap object?

[英]Bitmap object needed in MAUI.NET?

I need to store a bitmap object in my MAUI.NET application.我需要在我的 MAUI.NET 应用程序中存储一个 bitmap object。

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.要清楚 - 在我的定义中:bitmap 是由至少具有 R,G 和 B 值的二维像素阵列表示的图像。

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. 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?如何在 MAUI.NET 中处理这种情况?

If I understand your means correctly, you can paint graphical objects in the Microsoft.Maui.Graphics namespace.如果我正确理解您的意思,您可以在Microsoft.Maui.Graphics命名空间中绘制图形对象。

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.首先,可以使用DrawImage方法在ICanvas上绘制图像,该方法需要一个IImage参数,以及浮点类型的xywidthheight arguments 。

The following example shows how to load an image and draw it to the canvas:以下示例显示如何加载图像并将其绘制到 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.其次,您还可以绘制图像ImagePaint class 源自Paint class,用于绘制带有图像的图形 object。

The ImagePaint class defines an Image property, of type IImage , which represents the image to paint. ImagePaint class 定义了IImage类型的 Image 属性,它表示要绘制的图像。 The class also has an IsTransparent property that returns false. class 还具有返回 false 的IsTransparent属性。

To paint an object with an image, load the image and assign it to the Image property of the ImagePaint object.要使用图像绘制 object,请加载图像并将其分配给 ImagePaint object 的 Image 属性。

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有关更多详细信息,您可以查看: 绘制图形对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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