将Maui ImageSource转换为PNG

发布于 2025-01-27 02:47:09 字数 98 浏览 1 评论 0原文

我正在分别为Android,iOS和Windows的本机画布上渲染签名。现在,我需要在本地保存此保存,因此我将视图转换为流映像源格式。但是我不确定如何将其转换为PNG并在本地保存吗?

I'm rendering a signature on the native canvas for Android, iOS, and windows separately. Now, I need to save this locally, so I have converted the view to stream image source format. But I'm not sure how to convert to PNG and save it locally?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2025-02-03 02:47:09

您可以使用依赖项注入来处理它,请在您想要的平台上实现界面。

例如,我对Android进行了测试:

接口:

      public interface MyService
        {
            public  void Convert( string filename, ImageSource img);
        }
    }

实现:

 public class AndroidService : MyService
    {
        public async void Convert(string filename, ImageSource img)
        {
            System.IO.Stream outputStream = null;

            var handler = new FileImageSourceHandler(); 
            Bitmap pic = await handler.LoadImageAsync(img, Android.App.Application.Context);
          
          
            var savedImageFilename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), filename);
            bool success = false;
            using (outputStream = new System.IO.FileStream(savedImageFilename, System.IO.FileMode.Create))
            {
                if (System.IO.Path.GetExtension(filename).ToLower() == ".png")
                {
                    success = await pic.CompressAsync(Bitmap.CompressFormat.Png, 100, outputStream);
                }
                else
                    success = await pic.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, outputStream);
            }
        }
    }

You can use Dependency Injection to deal with it, implement the interface on the platform you want.

For example, I did a test on android:

The interface:

      public interface MyService
        {
            public  void Convert( string filename, ImageSource img);
        }
    }

The implemention:

 public class AndroidService : MyService
    {
        public async void Convert(string filename, ImageSource img)
        {
            System.IO.Stream outputStream = null;

            var handler = new FileImageSourceHandler(); 
            Bitmap pic = await handler.LoadImageAsync(img, Android.App.Application.Context);
          
          
            var savedImageFilename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), filename);
            bool success = false;
            using (outputStream = new System.IO.FileStream(savedImageFilename, System.IO.FileMode.Create))
            {
                if (System.IO.Path.GetExtension(filename).ToLower() == ".png")
                {
                    success = await pic.CompressAsync(Bitmap.CompressFormat.Png, 100, outputStream);
                }
                else
                    success = await pic.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, outputStream);
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文