我试图获取视图的屏幕截图如下.我正在获取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;
- }