Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, August 23, 2006 5:29 PM
Basically I am trying to create a Bitmap using the function
Bitmap(System.Int32 width, System.Int32 height, System.Int32 stride, System.Drawing.Imaging.PixelFormat format, System.IntPtr scan0)
Member of System.Drawing.Bitmap
but I cannot find anything on the web about converting byte arrays into IntPrt, so I can pass it as a argument. In the documentation it is said that IntPtr is a pointer to a array of bytes, and yet there si no method of converting from a byte array. So, does anyone have an answer to this one?
Thanks,
All replies (9)
Wednesday, August 23, 2006 6:11 PM
I think this is what you need. I would suggest you to stay with .NET API as long as possible and don't go to pure win32 APIs if possible
hope this helps
Wednesday, August 23, 2006 6:13 PM
CHeck out dat transformation methods in the Marshal Class![]()
Thursday, August 24, 2006 8:57 AM
I dont see how this can help me. The method returns a IntPtr from a Bitmap, and I need from byte array to IntPtr. So I dont see what you mean...
Thursday, August 24, 2006 9:04 AM
I did see this class and his methods, but I get a error from this code :
byte[] stream = new byte[16 * 16 * 3];
for (int i = 0; i < stream.Length; i++)
{
stream
= 100;
}
IntPtr ip = new IntPtr();
System.Runtime.InteropServices.Marshal.Copy(stream, 0, ip, stream.Length);
System.AccessViolationException on the last line, so I dont know what is the problem.
Thursday, August 24, 2006 9:08 AM
Yuo get this error because you are trying to write in memory directly. If you are absolutely sure you are right use unsafe keyword.
could you explain what are you trying to achieve and how it is related with Bitmap?
Thursday, August 24, 2006 12:49 PM | 1 vote
Zanzamar,
in your code the IntPtr ip doesn't point anywhere and that's probably what's causing the exception.
As an alternative approach, you can try to do the following:
- Create a bitmap with the desired size and the desired pixel format (from your example I assume you are using 24bpp).
- Use LockBits and Marshal to get the array of bytes.
- Change the array as you see fit.
- Marshal the array back and unlock the bits.
You can get a nice (and short) example from the MSDN. The obvious difference is that you don't have to load an existing image, so you should use a constructor like: "Bitmap bmp = new Bitmap (300, 200, PixelFormat.Format24bpp);" instead.
HTH
--mc
Tuesday, September 9, 2008 7:13 AM
Zanzamar,
Please read the following article:
http://blog.rednael.com/2008/08/29/MarshallingUsingNativeDLLsInNET.aspx
It's an in depth article about marshalling between native code and managed code. It shows which types are interoperable, how to import a DLL, how to pass strings, how to pass structures and how to de-reference pointers.
Basically all information you need, and some more :)
Tuesday, September 9, 2008 7:57 AM
Use GCHandle to pin the array and get an IntPtr to it. Then make your bitmap. You can also make a bitmap and use marshal copy to get and set the pixels.
Tuesday, March 8, 2011 9:23 PM
I use another constructor, the BMP format and a stream. Some of the lines of code are:
'Make the calculations of the size of the array plus the BMP header
ReDim
mg.BMPbytes(UnaImagen.TamañoBytesDeBmp(bytesPorPixel))
'copy header from another in memory BMP file
Array
.ConstrainedCopy(OtraImagen.BMPbytes, 0, mg.BMPbytes, 0, BMPenMemoria.BMP_FILE_HEADER_TAMAÑO)
mg.streamEnMemoria =
New MemoryStream
(mg.BMPbytes)
'Do not close the stream
mg.Imagen =
New Bitmap(Bitmap.FromStream(mg.streamEnMemoria))
BMP format, sorry for the spanish but it is available even in the wikipedia:
'BMP Format:
'[0] = 66d
'[1] = 77d
'[2] = LSB tamaño archivo
'[3] = tamaño archivo
'[4] = tamaño archivo
'[5] = tamaño archivo
'[A] = LSB Offset de la imagen
'[B] = offset de la imagen
'[C] = offset de la imagen
'[D] = offset de la imagen
'[E] = 40d tamaño del header standard tipo BITMAPINFOHEADER
'[12] = LSB ancho de la imagen
'[13-15] = ancho de la imagen
'[16] = LSB altura de la imagen
'[17-19] = altura de la imagen
'[1A] = 1 (color planes)
'[1C] = bits por pixel (1, 4, 8, 16, 24, 32)
'[22] = LSB tamaño de los datos de bits (cero en lo que regresa .Net)
'[23-25] tamaño del mapa de bits
'[26] = 196d LSB pixels per meter horizontal
'[27] = 14d pixels per meter horizontal
'[28-29] = 0d pixels por metro horizontal
'[2A] = 196d LSB pixels por metro vertical
'[2B] = 14d pixels por metro vertical
'[2C-2D] = pixels por metro vertical
'[2E-31] = 0 (para default 2 a la número de bits)
'[32-35] = 0 todos los bits importantes
'[36> ] Imagen. Última row primero. Redondeado cada renglón a 4 bytes.