ts-components-canvas-imagebitmap.md 1.4 KB
Newer Older
Z
zengyawen 已提交
1
# ImageBitmap
Z
zengyawen 已提交
2

E
esterzhou 已提交
3
An **ImageBitmap** object stores pixel data rendered on a canvas.
Z
zengyawen 已提交
4

E
esterzhou 已提交
5 6 7
> **NOTE**
> 
> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10



Z
zengyawen 已提交
11
## Attributes
Z
zengyawen 已提交
12

E
ester.zhou 已提交
13
| Name| Type| Description|
Z
zengyawen 已提交
14
| -------- | -------- | -------- |
E
ester.zhou 已提交
15 16
| width | number | Pixel width of the **ImageBitmap** object.|
| height | number | Pixel height of the **ImageBitmap** object.|
E
esterzhou 已提交
17 18 19 20 21 22 23 24

**Example**

  ```ts
  // xxx.ets
  @Entry
  @Component
  struct ImageExample {
E
ester.zhou 已提交
25 26 27
    private settings: RenderingContextSettings = new RenderingContextSettings(true)
    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
    private img:ImageBitmap = new ImageBitmap("common/images/example.jpg")
E
esterzhou 已提交
28 29 30 31 32 33 34 35

    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(() =>{
E
ester.zhou 已提交
36
            this.context.drawImage( this.img,0,0,500,500,0,0,400,200)
E
esterzhou 已提交
37 38 39 40 41 42 43 44 45 46
        })
      }
      .width('100%')
      .height('100%')
    }
  }
  ```

  ![en-us_image_0000001194352442](figures/en-us_image_0000001194352442.png)

E
ester.zhou 已提交
47 48 49 50 51 52 53 54 55 56


## Methods


### close

close()

Releases all graphics resources associated with this **ImageBitmap** object. This API is a void API.