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);
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With