Reputation: 93
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?
edit: Sorry if I wasn't clear. This is my scenario:
I am not about drawing and image in UI.
I want to let user specify path to file and then I need to have a Bitmap of this picture/image, because I need to pass it to different layers/project -> pass it to algorithms with the logic that would process it.
So this would be a code I would do in .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);
But in Maui.NET from a filedialog I managed to get something like below:
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 is an other .NET project that (for compatibility would also have .NET 6.0) - I guess this is required to work with MAUI.NET as dependency project.
There is Bitmap but seems that is dedicated only to Android: Android.Graphics.Bitmap - Can I use it in general code for all the platforms?
edit: The solution is SkiaSharp. Look into comments below for an alternative.
Upvotes: 4
Views: 8642
Reputation: 13949
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
Upvotes: 1