# Copying MakeCode image data from another application

**URL:** https://forum.makecode.com/t/copying-makecode-image-data-from-another-application/2656
**Category:** Show & Tell
**Created:** [July 3, 2020, 12:34am UTC](https://forum.makecode.com/t/copying-makecode-image-data-from-another-application/2656 "2020-07-03T00:34:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jacob\_c](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jacob_c/32/1532_2.png) [@jacob\_c](https://forum.makecode.com/u/jacob_c)
#### Post date: [July 3, 2020, 12:34am UTC](https://forum.makecode.com/t/copying-makecode-image-data-from-another-application/2656/1 "2020-07-03T00:34:29Z")

</div>

I saw in a recent stream that @jwunderl was mentioning the ability to copy + paste in the Sprite Editor on the arcade beta. … but sadly in the stream, they ended up having to resort to switching to JS to actually paste the image data in, since it was copied from another application.

Here’s a codesandbox that lets you get `img`…`` formatted plain text onto your clipboard with the clipboard format that the sprite editor is looking for: [https://codesandbox.io/embed/recursing-haze-642e2?fontsize=14&hidenavigation=1&theme=dark&view=preview](https://codesandbox.io/embed/recursing-haze-642e2?fontsize=14&hidenavigation=1&theme=dark&view=preview)

[Technical explanation: The current version of the sprite editor [puts two formats onto the clipboard](https://github.com/microsoft/pxt/blob/c9ac20f414a466a146007ff2fa66cc770f95c237/webapp/src/components/ImageEditor/ImageCanvas.tsx#L285-L286) when copying but only [accepts the `application/makecode-image` format](https://github.com/microsoft/pxt/blob/c9ac20f414a466a146007ff2fa66cc770f95c237/webapp/src/components/ImageEditor/ImageCanvas.tsx#L295) as pasted image data.]

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [July 3, 2020, 1:14am UTC](https://forum.makecode.com/t/copying-makecode-image-data-from-another-application/2656/2 "2020-07-03T01:14:03Z")

</div>

Ahhh, fantastic! I was just about to look at that, hah

Out of curiosity, did you have any particular reasons against changing [the paste logic](https://github.com/microsoft/pxt/blob/c9ac20f414a466a146007ff2fa66cc770f95c237/webapp/src/components/ImageEditor/ImageCanvas.tsx#L295) to

```typescript
const imageData = ev.clipboardData.getData('application/makecode-image') || ev.clipboardData.getData("text/plain");

```

so that it falls back to the plain text if previous copy wasn’t explicitly set as an image? Works fine (on my machine™) as the setdata properly clears the old data when another window’s copy event overrides it, though I suppose it does make it possible to end up pasting garbage as `imageLiteralToBitmap` just ignores any invalid characters.

kinda fun to paste random text in and see what image it converts to too, e.g. `it was the best of times ...` sentence from `A Tale of Two Cities` with linebreaks added in:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/9804c23fe6275c56d7919426b1b3fe7be6ba6933.png)

---

<div class="post-metadata">

### Author: ![jacob\_c](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jacob_c/32/1532_2.png) [@jacob\_c](https://forum.makecode.com/u/jacob_c)
#### Post date: [July 3, 2020, 1:24am UTC](https://forum.makecode.com/t/copying-makecode-image-data-from-another-application/2656/3 "2020-07-03T01:24:02Z")

</div>

Yes, one of the reasons was that I didn’t know the consequences of attempting to parse garbage data…

Another potential issue is that the paste handler is [registered with the `document`](https://github.com/microsoft/pxt/blob/c9ac20f414a466a146007ff2fa66cc770f95c237/webapp/src/components/ImageEditor/ImageCanvas.tsx#L127). By requiring the specific format, we only `preventDefault` pastes that we are sure we should handle, and avoid interfering with other valid paste destinations in the sprite editor (like the image dimensions inputs).

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [July 3, 2020, 1:50am UTC](https://forum.makecode.com/t/copying-makecode-image-data-from-another-application/2656/4 "2020-07-03T01:50:02Z")

</div>

Ahh, right, thanks! Well, there’s little downside to pasting garbage data as it will just generate a few random pixels that can be dragged away, and easy enough to do a quick check that it looks ~~ like an image literal before pasting. Put up a quick PR for that: [https://github.com/microsoft/pxt/pull/7270](https://github.com/microsoft/pxt/pull/7270)

---

<div class="post-metadata">

### Author: ![jacob\_c](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jacob_c/32/1532_2.png) [@jacob\_c](https://forum.makecode.com/u/jacob_c)
#### Post date: [July 7, 2020, 11:17pm UTC](https://forum.makecode.com/t/copying-makecode-image-data-from-another-application/2656/5 "2020-07-07T23:17:59Z")

</div>

Looks like Joey merged this PR today, so in an upcoming `/beta` release, you shouldn’t need to do the codesandbox dance to get ``img`…``` formatted data to paste into the sprite editor.

Thanks @jwunderl!
