【问题标题】:Convert Android.Graphics.Bitmap to Image将 Android.Graphics.Bitmap 转换为图像
【发布时间】:2015-10-14 23:08:16
【问题描述】:

我正在尝试获取视图的屏幕截图,如下所示。我正在获取位图,我需要将其转换为图像以添加到 PDF 生成器中。

using (var screenshot = Bitmap.CreateBitmap(200, 100,Bitmap.Config.Argb8888))
{
    var canvas = new Canvas(screenshot);
    rootView.Draw(canvas);

    using (var screenshotOutputStream = new FileStream(screenshotPath,System.IO.FileMode.Create))
    {
        screenshot.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 90, screenshotOutputStream);
        screenshotOutputStream.Flush();
        screenshotOutputStream.Close();
    }
}

我的问题是如何将Android.Graphics.Bitmap -->screenshot 转换为Image

我想用来自 BitMap 的图像本身替换 url。

String imageUrl = "myURL";
Image image = Image.GetInstance (new Uri (imageUrl));
document.Add(image);

【问题讨论】:

  • System.Drawing.Image 在 Xamarin Android 中不受支持。
  • 请查看我的三行代码 - 刚刚添加到我的问题中 - 在 Xamarin.Android 中工作。您是否有任何建议获取视图的屏幕截图并将其分配给图像以生成 pdf。
  • 这是一个 Android.Media.Image。抱歉,我误解了你的要求。

标签: android bitmap xamarin


【解决方案1】:

你可以用这个方法:

public Bitmap getScreenshotBmp() {


    FileOutputStream fileOutputStream = null;

    File path = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    String uniqueID = UUID.randomUUID().toString();

    File file = new File(path, uniqueID + ".jpg");
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    screenshot.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream);

    try {
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return screenshot;
 }

【讨论】:

  • 嗨 Udi,在我看来它仍然是Bitmap?我没有在代码中看到Image 转换?我错过了什么吗?
  • 这一行假设这样做:screenshot.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream);
  • screenshot 来自哪里?
  • 截图类型?
  • 问题有“xamarin”标签,为什么是Java?!
【解决方案2】:
Bitmap bm = BitmapFactory.DecodeFile (path);
MemoryStream stream = new MemoryStream ();
bm.Compress (Bitmap.CompressFormat.Jpeg, 100, stream);
Image img = Image.FromArray (stream.ToArray());

【讨论】:

  • 什么是 MemoryStream 和 Image.FromArray(..)?
  • android.media.Image 没有.FromArray()
猜你喜欢
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多