You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI是否支持Bitmap/Texture2D编辑?求SkiaSharp替代方案

回答

仅靠MAUI/.NET原生工具完全可以实现你的需求,不需要额外引入第三方框架。以下是具体实现方案及性能优化建议:

跨平台原生实现(基于Microsoft.Maui.Graphics)

MAUI自带的Microsoft.Maui.Graphics库提供了跨平台的Bitmap操作能力,无需依赖外部框架即可完成你的流程:

步骤示例代码

// 1. 拍摄照片
var photo = await MediaPicker.Default.CapturePhotoAsync();
if (photo == null) return;

// 2. 加载为可操作的Bitmap
using var stream = await photo.OpenReadAsync();
using var bitmap = await Bitmap.LoadAsync(stream);

// 3. 标记内容(示例:在(100,100)位置绘制100x100的红色块)
using var context = bitmap.CreateBitmapContext();
var redPixel = Colors.Red.ToUInt32();
var width = bitmap.Width;
var height = bitmap.Height;

// 批量修改像素(比逐个SetPixel更高效)
for (int x = 100; x < Math.Min(200, width); x++)
{
    for (int y = 100; y < Math.Min(200, height); y++)
    {
        context.Pixels[y * width + x] = redPixel;
    }
}

// 4. 赋值给图像控件
imageControl.Source = ImageSource.FromStream(() => 
{
    var ms = new MemoryStream();
    bitmap.Save(ms, ImageFormat.Png);
    ms.Position = 0;
    return ms;
});

平台特定优化(极致性能)

如果跨平台方案的性能仍无法满足需求,可以针对不同平台使用原生Bitmap API,直接调用底层接口避免跨层开销:

Android平台

#if ANDROID
var photo = await MediaPicker.Default.CapturePhotoAsync();
if (photo == null) return;

using var stream = await photo.OpenReadAsync();
var androidBitmap = Android.Graphics.BitmapFactory.DecodeStream(stream);

// 直接使用原生SetPixel或批量操作像素数组
androidBitmap.SetPixel(100, 100, Android.Graphics.Color.Red);
// 批量修改示例:获取像素数组后直接操作
int[] pixels = new int[androidBitmap.Width * androidBitmap.Height];
androidBitmap.GetPixels(pixels, 0, androidBitmap.Width, 0, 0, androidBitmap.Width, androidBitmap.Height);
// 修改pixels数组...
androidBitmap.SetPixels(pixels, 0, androidBitmap.Width, 0, 0, androidBitmap.Width, androidBitmap.Height);

// 转换为MAUI ImageSource
imageControl.Source = ImageSource.FromStream(() => 
{
    var ms = new MemoryStream();
    androidBitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, ms);
    ms.Position = 0;
    return ms;
});
#endif

iOS平台

#if IOS
var photo = await MediaPicker.Default.CapturePhotoAsync();
if (photo == null) return;

using var stream = await photo.OpenReadAsync();
var uiImage = UIKit.UIImage.LoadFromData(Foundation.NSData.FromStream(stream));

// 使用CoreGraphics直接绘制标记
UIGraphics.BeginImageContext(uiImage.Size);
uiImage.Draw(CoreGraphics.CGRect.Empty);
var cgContext = UIGraphics.GetCurrentContext();
cgContext.SetFillColor(UIKit.UIColor.Red.CGColor);
cgContext.FillRect(new CoreGraphics.CGRect(100, 100, 100, 100));
var modifiedImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();

// 转换为MAUI ImageSource
imageControl.Source = ImageSource.FromStream(() => 
{
    var ms = new MemoryStream();
    modifiedImage.AsPNG().AsStream().CopyTo(ms);
    ms.Position = 0;
    return ms;
});
#endif

关于SkiaSharp性能问题的补充

你遇到的SkiaSharp性能差,大概率是因为采用了逐个SetPixel的低效操作方式。如果仍想使用SkiaSharp,建议:

  • 直接操作SKBitmap的像素数组(SKBitmap.Pixels)批量修改,而非逐个调用SetPixel
  • 使用SKCanvas进行绘制操作,利用硬件加速能力

内容的提问来源于stack exchange,提问作者ChiefGardener

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 09:35:14