How to get the memory size of a byte array in VB.net?

J

Jon Finch

Hi All this is a follow up to a post I did a few days back regarding reading
a font from an assembly into a memory font object.

The only thing I am stuck on is getting the actual memory size of a byte
array so that I can allocate the memory for the pointer..

Here's the code

'Load the font from the assembly

Public myFonts As New PrivateFontCollection()

Dim asm As [Assembly] = Me.GetType.Assembly

Dim stm As Stream =
asm.GetManifestResourceStream("CreativeTools.FtraBk__.ttf")

Dim buffer(Convert.ToInt32(stm.Length)) As Byte

stm.Read(buffer, 0, Convert.ToInt32(stm.Length))
buffer.Length) <<

Marshal.Copy(buffer, 0, pi, buffer.Length)

myFonts.AddMemoryFont(pi, buffer.Length)

Marshal.FreeHGlobal(pi)

--

The line I am having problems with is the

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(byte) *
buffer.Length)

I have tried

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer) *
buffer.Length)

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer.getType) *
buffer.Length)

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf( buffer.Length) )

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer)

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf([Byte]) *
buffer.Length)

All to no avail! So how do I get the memory size of a byte array??

Any help would be greatly appreciated

All the best,

Jon
 
C

Christine Nguyen

Hi,

If you know buffer is a byte array, why can't you just call

Dim pi As IntPtr = Marshal.AllocHGlobal(buffer.Length). Buffer.Length
basically gives you the number of bytes to allocate in memory. Unless I'm
missing something....

-Christine

Jon Finch said:
Hi All this is a follow up to a post I did a few days back regarding reading
a font from an assembly into a memory font object.

The only thing I am stuck on is getting the actual memory size of a byte
array so that I can allocate the memory for the pointer..

Here's the code

'Load the font from the assembly

Public myFonts As New PrivateFontCollection()

Dim asm As [Assembly] = Me.GetType.Assembly

Dim stm As Stream =
asm.GetManifestResourceStream("CreativeTools.FtraBk__.ttf")

Dim buffer(Convert.ToInt32(stm.Length)) As Byte

stm.Read(buffer, 0, Convert.ToInt32(stm.Length))
buffer.Length) <<

Marshal.Copy(buffer, 0, pi, buffer.Length)

myFonts.AddMemoryFont(pi, buffer.Length)

Marshal.FreeHGlobal(pi)

--

The line I am having problems with is the

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(byte) *
buffer.Length)

I have tried

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer) *
buffer.Length)

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer.getType) *
buffer.Length)

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf( buffer.Length) )

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer)

Dim pi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf([Byte]) *
buffer.Length)

All to no avail! So how do I get the memory size of a byte array??

Any help would be greatly appreciated

All the best,

Jon
 
J

Jon Finch

Hi,
If you know buffer is a byte array, why can't you just call

Dim pi As IntPtr = Marshal.AllocHGlobal(buffer.Length). Buffer.Length
basically gives you the number of bytes to allocate in memory. Unless I'm
missing something....

-Christine

Thanks for your reply,

I believe calling buffer.length will only give me the number of elements in
the byte array which I then need to multiply by the amount of memory a byte
structure would take up.
Does anyone know how much a single one element byte array structure would
take up in memory (i'm assuming it's not just 8 bits and that there is some
overhead to the structure itself though I could be wrong).

All the best,

Jon
 
M

Mattias Sjögren

Jon,
I believe calling buffer.length will only give me the number of elements in
the byte array which I then need to multiply by the amount of memory a byte
structure would take up.
Does anyone know how much a single one element byte array structure would
take up in memory (i'm assuming it's not just 8 bits and that there is some
overhead to the structure itself though I could be wrong).


A Byte takes up exactly one byte. There's no overhead for value types.



Mattias
 
J

Jon Finch

A Byte takes up exactly one byte. There's no overhead for value types.



Mattias

Thanks Mattias,

Anyway I managed to get the font to load using
Marshal.AllocHGlobal(Marshal.sizeOf(GetType(Byte)) * myBuffer.length) which
as both you and Christine pointed out is the same as myBuffer.length anyway
as SizeOf(GetType(Byte) returned 1 ;-)

I thought it wasn't loading as the font looked incorrect in the labels. I
have found that when the font (Futura Book) is loaded dynamically this way
it does not display properly under 14 points (no antialiasing) and actually
appears as italic in Labels. (Which is very strange) Even when you tell it
to go to regular. However, it does appear correctly when you use the
Graphics.DrawString functionality so I can only assume this is a bug of some
sort.

Anyway the long and the short of it is that I am now using the GDI+
functionality to draw bitmaps onto the labels instead of using the Labels
Text item. It's a bit more of a pain as I'll have to go through and update
the entire application but it's better than not having the correct font.

Thanks for your help Christine and Mattias.

Jon

p.s.
Has anyone else had any troubles with loading fonts from memory and them not
displaying correctly under 14 points?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top