【问题标题】:Bitmap object needed in MAUI.NET?MAUI.NET 中需要位图对象吗?
【发布时间】:2022-09-23 02:56:16
【问题描述】:

我需要在我的 MAUI.NET 应用程序中存储一个位图对象。

要清楚 - 在我的定义中:位图是由至少具有 R、G 和 B 值的二维像素数组表示的图像。

在 .NET 4.7 中,它还不是这样的对象,但是有一个 NuGet System.Drawings.Common 允许我使用这样的对象。

如何在 MAUI.NET 中处理这种情况?

编辑: 抱歉,如果我不清楚。这是我的场景:

我不是在 UI 中绘图和图像。

我想让用户指定文件的路径,然后我需要这个图片/图像的位图,因为我需要将它传递给不同的图层/项目 - >将它传递给具有处理它的逻辑的算法。

所以这将是我将在 .NET Framework 4 中执行的代码......:

string filepath = someFileSystemDialog.Result;
Bitmap bitmap = new Bitmap(filepath); // here is the problem, in previous .NET there was Bitmap object wchich was perfect, here is lack
Bitmap processedBitmap = MyOtherProjectWithLogicAlgorithms.ProcessAnImage(bitmap);
processedBitmap.Save(finalOutputPath, ImageFormat.Png);

但是在 Maui.NET 中,我从文件对话框中得到了如下信息:

var fileResult = await FilePicker.Default.PickAsync(...);
    if(fileResult != null)
    {
        ImageSource is = ImageSource.FromFile(fileResult.FullPath);
        Bitmap bm = ??(is); // how to get Bitmap from an ImageSource ?      

MyOtherProjectWithLogicAlgorithms 是另一个 .NET 项目(为了兼容性也将具有 .NET 6.0) - 我想这是将 MAUI.NET 作为依赖项项目使用所必需的。

  • 你能分享一些代码吗?
  • 使用 SkiaSharp 进行跨平台图形

标签: c# bitmap maui c#-6.0 .net-maui


【解决方案1】:

如果我正确理解您的意思,您可以在 Microsoft.Maui.Graphics 命名空间中绘制图形对象。

第一的, 可以使用DrawImage 方法在ICanvas 上绘制图像,该方法需要IImage 参数,以及x、y、width 和height 参数,类型为float。

以下示例显示了如何加载图像并将其绘制到画布上:

  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);
}

更多信息,您可以查看:Draw an image。

第二,您也可以绘制图像。从Paint class 派生的ImagePaint 类用于绘制带有图像的图形对象。

ImagePaint 类定义了一个 IImage 类型的 Image 属性,它表示要绘制的图像。该类还有一个返回 false 的 IsTransparent 属性。

要使用图像绘制对象,请加载图像并将其分配给 ImagePaint 对象的 Image 属性。

以下示例显示如何加载图像并用它填充矩形:

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);
}

更多详情可以查看:Paint graphical objects

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2013-09-23
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多