# Does StreamImage have a function like ImageToMemoryRaw

**URL:** <https://forum.commonvisionblox.com/t/does-streamimage-have-a-function-like-imagetomemoryraw/639>\
**Category:** .Net API\
**Created:** [January 3, 2019, 4:51pm UTC](https://forum.commonvisionblox.com/t/does-streamimage-have-a-function-like-imagetomemoryraw/639 "2019-01-03T16:51:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![soultower](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@soultower](https://forum.commonvisionblox.com/u/soultower)\
**Post date:** [January 3, 2019, 4:51pm UTC](https://forum.commonvisionblox.com/t/does-streamimage-have-a-function-like-imagetomemoryraw/639/1 "2019-01-03T16:51:13Z")

</div>

Does StreamImage have a function like ImageToMemoryRaw to transfer images to other visual processing software?

---

<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:** [January 4, 2019, 8:01am UTC](https://forum.commonvisionblox.com/t/does-streamimage-have-a-function-like-imagetomemoryraw/639/2 "2019-01-04T08:01:30Z")

</div>

Hi @soultower,

`ImageToMemoryRaw` is currently not exported through the [CVB.Net](http://CVB.Net) API and I think it is not necessarily the best solution for what you have in mind because it will _always_ copy the image to into a block of memory. Most of the time when trying to interface to another software this is over the top - such copy operations are something most people will try to avoid as good as possible.

If you look at different software packages, more often than not a combination of width, height, base pointer and y increment is necessary to describe the memory layout of an image to a processing function, and for :cvb: images these are most often accessible without producing a deep copy of the image data. Every entry in the `Stemmer.Cvb.Image.Planes` collection has a method `TryGetLinearAccess()` (as well as its throw-on-error sibling `GetLinearAccess()`). If accessing the image’s pixels in a linear fashion (in other words: if a pixel’s memory offsets depend linearly on the pixel’s x and y position) this function will fill a `Stemmer.Cvb.LinearAccessData` structure for you that contains the base pointer (`BasePtr`), the x increment (`XInc`) and the y increment (`YInc`). Width and height are accessible through `Stemmer.Cvb.Image.Width` and `Stemmer.Cvb.Image.Height`. Usually this data does the trick - especially if the image comes from a camera. Just make sure you don’t `Dispose()` your image while the other library is still working on it.

If, on the other hand, linear access is _not_ possible (`TryGetLinearAccess()` returns `false` or `GetLinearAccess()` raises a `System.FormatException`) then there will be no way around a deep copy of the image data before passing it to a different library (:cvb: is capable of processing the weirdest kinds of memory layout while almost _all_ other libraries process only linearly arranged pixel data; luckily weird arrangements have become rare these days 🙂). In that case I’d recommend using the `Clone()` method to prepare a deep copy of the data into a new `Stemmer.Cvb.Image` and access this one through `TryGetLinearAccess()` (the performance hit due to the deep copy will be identical to what `ImageToMemoryRaw()` would require.

`ImageToMemoryRaw()` will sooner or later be added to [CVB.Net](http://CVB.Net) as well - you can expect to see it in the 13.02.000 release of the library.

---

<div class="post-metadata">

**Author:** ![soultower](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@soultower](https://forum.commonvisionblox.com/u/soultower)\
**Post date:** [January 4, 2019, 11:04am UTC](https://forum.commonvisionblox.com/t/does-streamimage-have-a-function-like-imagetomemoryraw/639/3 "2019-01-04T11:04:27Z")

</div>

`ImageToMemoryRaw()`I need it very much. When will 13.02 be released?

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [January 4, 2019, 7:26pm UTC](https://forum.commonvisionblox.com/t/does-streamimage-have-a-function-like-imagetomemoryraw/639/4 "2019-01-04T19:26:55Z")

</div>

You can use it even today if you reference the _iCVCImg.dll_:

```cs
static class ImageExtensions
{
  public static unsafe byte[] ToMemoryBuffer(this Image image)
  {
    if (image == null)
      throw new ArgumentNullException(nameof(image));

    var sizeInBytes = Cvb.Image.ImageToMemoryRawSize(image.Handle);
    var buffer = new byte[sizeInBytes];
    fixed (byte* bufferPtr = buffer)
    {
      if (!Cvb.Image.ImageToMemoryRaw(image.Handle, (IntPtr)bufferPtr, sizeInBytes, Cvb.Image.TRawFileLayout.RFL_Interleaved))
        throw new Exception($"Could not create raw image buffer from {nameof(image)}");
    }
    return buffer;
  }
}

```

You can use it like this:

```cs
var buffer = myImage.ToMemoryBuffer();

```
