Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Android.Graphics.Bitmap to Image

I am trying to get screenshot of the view as follows. I am getting Bitmap and I need to convert it to to Image to add into PDF generator.

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();
    }
}

My question is how to convert Android.Graphics.Bitmap -->screenshot to Image ?

I want to replace the url with Image itself which comes from BitMap.

String imageUrl = "myURL";
Image image = Image.GetInstance (new Uri (imageUrl));
document.Add(image);
like image 995
casillas Avatar asked Oct 13 '15 23:10

casillas


1 Answers

You can use this method:

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;
 }
like image 183
Udi Idan Avatar answered Sep 30 '22 20:09

Udi Idan