# Is CreateImageFromPointer available for .Net?

**URL:** <https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805>\
**Category:** .Net API\
**Created:** [June 6, 2019, 2:02pm UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805 "2019-06-06T14:02:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Ben](https://forum.commonvisionblox.com/u/Ben)\
**Post date:** [June 6, 2019, 2:02pm UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805/1 "2019-06-06T14:02:01Z")

</div>

Sorry if it’s obvious, but I haven’t found it. I don’t _think_ Image.FromHandle is exactly the same?

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [June 6, 2019, 2:50pm UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805/2 "2019-06-06T14:50:48Z")

</div>

`CreateImageFromPointer` is what is behind the `WrappedImage` ctors. Note that the buffers should be either unmanaged or fixed, otherwise you’ll quickly see access violations 😉

---

<div class="post-metadata">

**Author:** ![Ben](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Ben](https://forum.commonvisionblox.com/u/Ben)\
**Post date:** [June 7, 2019, 5:11am UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805/3 "2019-06-07T05:11:29Z")

</div>

I don’t _see_ any ctors for `MappedImage` though ☹

For context: I’m trying to do the conversion from OpenCV back to :cvb: (because I created the `OpenCV.Mat` using a pointer, so it thinks my RGB data is BGR, and will invert it if I use `Mat.Save`, so I want to map _back_ to Image so I can save!), so I’m looking for the C# equivalent of what the CVBInterop.pdf describes.

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [June 7, 2019, 6:33am UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805/4 "2019-06-07T06:33:48Z")

</div>

My bad 🤦‍♂️  
It’s `WrappedImage`, not `MappedImage`…

---

<div class="post-metadata">

**Author:** ![Ben](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Ben](https://forum.commonvisionblox.com/u/Ben)\
**Post date:** [June 7, 2019, 7:03am UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805/5 "2019-06-07T07:03:20Z")

</div>

I’m just trying roundtripping (which may be dangerous) and have an AccessViolationException on the last line. Is it because of what I’m trying to do, or because of misuse of the parameters?

```auto
//Image img from camera (PixelFormat = RGB8)
var access = img.Planes[0].GetLinearAccess();
var mat = new Mat(img.Height, img.Width, DepthType.Cv8U, img.Planes.Count, 
                  access.BasePtr, (int)access.YInc);
var size = mat.Cols * mat.Rows * mat.ElementSize;
WrappedImage.FromRgbPixels(mat.Ptr, size, mat.Cols, mat.Rows, PixelDataType.UInt, 
                           8, 3, mat.Step, 1);

```

---

<div class="post-metadata">

**Author:** ![Ben](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Ben](https://forum.commonvisionblox.com/u/Ben)\
**Post date:** [June 7, 2019, 7:20am UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805/6 "2019-06-07T07:20:53Z")

</div>

Haha!  
I found my bug already `mat.Ptr` is a pointer to the start of the object, you need to use `mat.DataPointer` for the start of the data. So the corrected code is:

```auto
//Image img from camera (PixelFormat = RGB8)
var access = img.Planes[0].GetLinearAccess();
var mat = new Mat(img.Height, img.Width, DepthType.Cv8U, img.Planes.Count, 
                  access.BasePtr, (int)access.YInc);
var size = mat.Cols * mat.Rows * mat.ElementSize;
WrappedImage.FromRgbPixels(mat.DataPointer, size, mat.Cols, mat.Rows, 
                           PixelDataType.UInt, 8, 3, mat.Step, 1);

```

Now I just need to clear up some of those magic numbers…

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [June 7, 2019, 1:40pm UTC](https://forum.commonvisionblox.com/t/is-createimagefrompointer-available-for-net/805/7 "2019-06-07T13:40:16Z")

</div>

😄  
sorry, did not get around to looking at this earlier - and thanks for sharing the solution! 👍
