将Android.Graphics.Bitmap转换为Image

9

我试图获取以下视图的屏幕截图。我已经得到了位图,现在需要将其转换为图像以添加到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 的 Image 替换 url。
String imageUrl = "myURL";
Image image = Image.GetInstance (new Uri (imageUrl));
document.Add(image);

Xamarin Android 不支持 System.Drawing.Image。 - Jason
请查看我在问题中添加的三行代码 - 它们在 Xamarin.Android 中运行。您有没有任何建议可以获取视图的屏幕截图并将其分配给图像以生成 PDF。 - casillas
1
那是一个Android.Media.Image。抱歉,我误解了你的问题。 - Jason
1个回答

5
你可以使用这种方法:
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;
 }

1
嗨Udi,对我来说它似乎仍然是“Bitmap”?我没有在代码中看到“Image”转换?我错过了什么吗? - casillas
1
这行代码应该是这样的: screenshot.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream); - SuperFrog
2
screenshot 是从哪里来的? - Roy Shilkrot
截图类型? - full Error
这个问题有“xamarin”标签,为什么是Java?! - Adam Williams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接