【问题标题】:C# / Xamarin: Saving image and Sharing on WhatsappC# / Xamarin:在 Whatsapp 上保存图像和共享
【发布时间】:2021-08-09 18:00:04
【问题描述】:

我正在尝试从 Bitmap 保存图像并在 whatsapp 中分享,但是当我将其保存在手机中并尝试打开它时,图像变黑了,所以,当我分享它时,我分享了一个黑色图像。在我测试的其他手机中,我可以打开保存的图像,但在我正在测试的这款手机中,我不能。两者都是相同的Android版本。 在这个出现黑色图像的手机中,我可以在预览(图库)中看到照片,但是当我尝试打开它时它变黑了。

这是我保存位图的代码:

 public SKBitmap SaveImage(float ySize, SKBitmap bmpFormatted, SKBitmap standardBmp, int SKCanvasHeight)
        {
            float width = (float)screenSize.Width;
            bmpFormatted = new SKBitmap((int)width, SKCanvasHeight);
            SKRectI rect = new SKRectI(0, 0, (int)width, (int)ySize);
            standardBmp.ExtractSubset(bmpFormatted, rect);

            SKData data = bmpFormatted.Encode(SKEncodedImageFormat.Png, 100);
            byte[] bmpByte = data.ToArray();
            string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
            string path = Path.Combine(directory, "bmpSaved.png");

            File.WriteAllBytes(path, bmpByte);

            return bmpFormatted;
        }

这是我分享保存位图的代码:

string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
            string path = Path.Combine(directory, "bmpSaved.png");

            var _context = Android.App.Application.Context;

            Intent sendIntent = new Intent(global::Android.Content.Intent.ActionSend);

            sendIntent.PutExtra(global::Android.Content.Intent.ExtraText, "Whatsapp");
            sendIntent.SetType("image/*");

            sendIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + path));
            _context.StartActivity(Intent.CreateChooser(sendIntent, "Share"));
            return Task.FromResult(0);

有什么区别或其他方法吗?因为我需要它在两部手机中都可以使用。 有什么想法吗?

【问题讨论】:

  • 将不工作的手机连接到计算机,开始调试 - 这样可以在 Visual Studio 的输出窗格中看到日志消息。保存图像时是否有任何警告或错误?在byte[] bmpByte = ... 之后的行放置一个断点。开始时应该有一些非零字节(标头信息),但在某一点上,这些数据是否全部变为 0(黑色)? bmpByte 的长度在两部手机上是否相同?鉴于它可以在一部手机上运行,​​您需要调试两部手机上发生的情况,准确找出哪一行代码具有不同的值。
  • “我可以在预览(图库)中看到照片,但是当我尝试打开它时它变黑了。” - width 的值是什么和ySize 在两部手机上?在完成File.WriteAllBytes(path, bmpByte) 之后,使用FileInfo.Length(path) - 该值是否至少与bmpByte.Length 一样大?
  • 您要保存的图像的来源是什么?您是否使用某种外部图像编辑器检查了“坏”图像,看看它与“好”图像有何不同?
  • 使用 adb 复制到桌面
  • 好的,先生们,我找到了答案。我不知道为什么,但我所做的这种类型的保存图像不适用于某些类型的手机(??),所以我从这个链接做了另一个,它在两部手机上都有效:stackoverflow.com/a/59393550/14700237。如果有人遇到与我相同的问题,我会回答。感谢您的回答和支持。

标签: c# xamarin xamarin.forms bitmap skiasharp


【解决方案1】:

所以如果有人和我有同样的问题,试试这个(对我来说它有效):

using (var image = surface.Snapshot())
            using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
            using (var stream = File.OpenWrite(Path.Combine(directory, "1.png")))
            {
                // save the data to a stream
                data.SaveTo(stream);
            }

感谢:https://stackoverflow.com/a/59393550/14700237

【讨论】:

    猜你喜欢
    • 2014-09-05
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多