将Android.Graphics.Bitmap转换为Image

前端之家收集整理的这篇文章主要介绍了将Android.Graphics.Bitmap转换为Image前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图获取视图的屏幕截图如下.我正在获取Bitmap,我需要将其转换为 Image添加到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 – >截图转换为Image?

我想用来自BitMap的Image本身替换url.

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

解决方法

您可以使用此方法
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;
 }
原文链接:https://www.f2er.com/android/309773.html

猜你在找的Android相关文章