将Android.Graphics.Bitmap转换为Image

前端之家收集整理的这篇文章主要介绍了将Android.Graphics.Bitmap转换为Image前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图获取视图的屏幕截图如下.我正在获取Bitmap,我需要将其转换为 Image添加到PDF生成器中.
  1. using (var screenshot = Bitmap.CreateBitmap(200,100,Bitmap.Config.Argb8888))
  2. {
  3. var canvas = new Canvas(screenshot);
  4. rootView.Draw(canvas);
  5.  
  6. using (var screenshotOutputStream = new FileStream(screenshotPath,System.IO.FileMode.Create))
  7. {
  8. screenshot.Compress(Android.Graphics.Bitmap.CompressFormat.Png,90,screenshotOutputStream);
  9. screenshotOutputStream.Flush();
  10. screenshotOutputStream.Close();
  11. }
  12. }

我的问题是如何将Android.Graphics.Bitmap – >截图转换为Image?

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

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

解决方法

您可以使用此方法
  1. public Bitmap getScreenshotBmp() {
  2.  
  3.  
  4. FileOutputStream fileOutputStream = null;
  5.  
  6. File path = Environment
  7. .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  8.  
  9. String uniqueID = UUID.randomUUID().toString();
  10.  
  11. File file = new File(path,uniqueID + ".jpg");
  12. try {
  13. fileOutputStream = new FileOutputStream(file);
  14. } catch (FileNotFoundException e) {
  15. e.printStackTrace();
  16. }
  17.  
  18. screenshot.compress(Bitmap.CompressFormat.JPEG,30,fileOutputStream);
  19.  
  20. try {
  21. fileOutputStream.flush();
  22. fileOutputStream.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. return screenshot;
  27. }

猜你在找的Android相关文章