# Using Paintable Canvas to get Blob image from MySQL

**URL:** <https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354>\
**Category:** Ignition\
**Tags:** scripting, ignition81, vision\
**Created:** [September 19, 2023, 4:56pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354 "2023-09-19T16:56:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Houston](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nicholas_houston/32/68700_2.png) [@Nicholas\_Houston](https://forum.inductiveautomation.com/u/Nicholas_Houston)\
**Post date:** [September 19, 2023, 4:56pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/1 "2023-09-19T16:56:59Z")

</div>

I am trying to get an image from a BLOB column in ignition. I have an image stored in MySQL in a blob column currently and am trying to display it with the paintable canvas in Ignition Vision 8.1. The code I am using currently is not working. Any help would be appreciated!

 ![Capture](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/f/1f9f3277aeddfd912bf9d6aa5a6eca5cef712fe3.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 19, 2023, 4:59pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/2 "2023-09-19T16:59:23Z")

</div>

> [@Nicholas\_Houston](#):
>
> is not working

What does happen? Any errors in the console? Your approach there is still relatively inefficient, by the way - ImageIO's construction of the image isn't free and is going to be called often. It'd be better to cache the actual Image instance.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Houston](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nicholas_houston/32/68700_2.png) [@Nicholas\_Houston](https://forum.inductiveautomation.com/u/Nicholas_Houston)\
**Post date:** [September 19, 2023, 5:02pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/3 "2023-09-19T17:02:15Z")

</div>

There aren't any errors in the console. When I put the designer into preview mode nothing shows up for the paintable canvas, and no errors are put into the console.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [September 19, 2023, 5:04pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/4 "2023-09-19T17:04:54Z")

</div>

This may help simplify your efforts  
[Automation Professionals' Blob Server Module - 3rd Party Modules - Inductive Automation Forum](https://forum.inductiveautomation.com/t/automation-professionals-blob-server-module/57970)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 19, 2023, 6:10pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/5 "2023-09-19T18:10:21Z")

</div>

> [@dkhayes117](#):
>
> This may help simplify

Not necessary for Vision. Vision can call a named query natively.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 19, 2023, 6:12pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/6 "2023-09-19T18:12:13Z")

</div>

Pssst! Don't run a query inside a repaint event! Run the query as a named query binding on a custom property. Then the repaint event can just read the bytes. (And draw nothing when no bytes are present.)

---

<div class="post-metadata">

**Author:** ![Nicholas\_Houston](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nicholas_houston/32/68700_2.png) [@Nicholas\_Houston](https://forum.inductiveautomation.com/u/Nicholas_Houston)\
**Post date:** [September 19, 2023, 6:42pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/7 "2023-09-19T18:42:23Z")

</div>

For anyone that comes into this to check for solution. I ended up getting it to work by defining column and row index from sql query return. Also used @pturmel suggestion and placed the query inside a custom property.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/f/cfd8c32e2e8343b86a2832ecfd4e8afe604d3599.png)

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [October 24, 2024, 9:18pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/8 "2024-10-24T21:18:17Z")

</div>

This was very helpful to render an image blob from a database table, thanks! Is there a way to get the image rendered to resize to fit the canvas?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 24, 2024, 9:34pm UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/9 "2024-10-24T21:34:29Z")

</div>

You can generate an `AffineTransform` from the sizes of the image and the graphics space, then use this drawImage overload:

[https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics2D.html#drawImage(java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver)](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics2D.html#drawImage(java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver))

Or this one:

[https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics.html#drawImage(java.awt.Image,int,int,int,int,int,int,int,int,java.awt.image.ImageObserver)](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics.html#drawImage(java.awt.Image,int,int,int,int,int,int,int,int,java.awt.image.ImageObserver))

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [October 26, 2024, 8:40am UTC](https://forum.inductiveautomation.com/t/using-paintable-canvas-to-get-blob-image-from-mysql/79354/10 "2024-10-26T08:40:06Z")

</div>

> [@richardNZ15](#):
>
> get the image rendered to resize to fit the canvas?

Perhaps more simplistically stated, the width and height parameters can be specified within the `drawImage` call.

**Example:**

```python
# graphics.drawImage(image, x, y, width, height, observer)
graphics.drawImage(image, 0, 0, event.width, event.height, event.source)

```

**Note:** If graphics.scale(dX, dY) is being used, the width and height variables will need to be explicit to the scaled size  
**Example:**

```python
dX = (event.width-1)/100.0
dY = (event.height-1)/100.0
graphics.scale(dX,dY)

# graphics.drawImage(image, x, y, width, height, observer)
graphics.drawImage(image, 0, 0, 100, 100, event.source)

```
