将bitmap转为image
时间: 2023-08-31 13:06:37 浏览: 67
可以使用 Image 类的 FromBitmap() 方法将 Bitmap 类型的图像转换为 Image 类型的图像,示例代码如下:
```
Bitmap bitmap = new Bitmap("test.bmp");
Image image = Image.FromBitmap(bitmap);
```
在上述代码中,首先创建了一个 Bitmap 对象,然后使用 FromBitmap() 方法将其转换为 Image 对象。需要注意的是,如果 Bitmap 包含的像素格式不是 GDI+ 支持的像素格式,则需要将其转换为 GDI+ 支持的像素格式,否则可能会导致转换失败。
相关问题
drawable转为bitmap的方法
可以使用BitmapFactory类的decodeResource方法将drawable转为bitmap,示例代码如下:
``` java
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
```
其中,R.drawable.image是要转换的drawable资源。可以将其放在ImageView或Bitmap对象中使用。
kotlin中把图片转为bitmap
在 Kotlin 中,可以使用以下代码将图片转换为 Bitmap:
``` kotlin
val bitmap = BitmapFactory.decodeFile("path/to/image.jpg")
```
其中,`"path/to/image.jpg"` 是图片文件的路径。如果你要将 Drawable 或者资源文件中的图片转换为 Bitmap,可以使用以下代码:
``` kotlin
val drawable = resources.getDrawable(R.drawable.image)
val bitmap = (drawable as BitmapDrawable).bitmap
```
其中,`R.drawable.image` 是图片资源的 ID。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)