[2005] Creating a Pointer to a Array of Bytes
Hi!!!
I have this code:
vb Code:
Dim TGA As New TGAReader(Open.FileName)
Dim imagedata() As Byte = TGA.GetImageData(TGA.FileBytes)
Pic.Image = New Bitmap(TGA.ImageWidth, TGA.ImageHeight, TGA.ImageStride, Imaging.PixelFormat.Format24bppRgb, 'The pointer here)
The TGAReader class is a class made by me that reads a TGA file. As suggested the Method GetImageData retrieves a array of type byte.
This array contains three bytes for each pixel (The RGB intensities), thats why the pixelformat is 24bppRgb.
Now i want to create a pointer to that array of bytes. But i just dont know how. I know that a pointer can "be created" throw the IntPtr class. But I dont have any value to pass to the constructer. Can you help me??
Re: [2005] Creating a Pointer to a Array of Bytes
Does VB support pointers like these? I dont think so..C# does though.
Re: [2005] Creating a Pointer to a Array of Bytes
Watch this code:
vb Code:
Dim Bitma As New Bitmap(Open.FileName)
Dim imageData As System.Drawing.Imaging.BitmapData
imageData = Bitma.LockBits(New Rectangle(0, 0, Bitma.Width, Bitma.Height), Imaging.ImageLockMode.ReadWrite, Bitma.PixelFormat)
Pic.Image = New Bitmap(Bitma.Width, Bitma.Height, imageData.Stride, Bitma.PixelFormat, New IntPtr(CInt(imageData.Scan0)))
It reads a .jpg image and shows it using a pointer.
I want to do the same to my image. But my problem is that my image is TGA, and VB.net doesn't natively supports it, so i have to read the bytes where the pixel information is, and then create the image. I've tried the setpixel, but it takes like ages.
Re: [2005] Creating a Pointer to a Array of Bytes
A Byte Array is a reference type so just pass the byte array to the constructor By Value (ByVal).
Code:
Public Sub New(ByVal myByteArray As Byte())
End Sub
This will pass a copy of a pointer to your byte array to the constructor.
Re: [2005] Creating a Pointer to a Array of Bytes
Quote:
Originally Posted by Lasering
Watch this code:
vb Code:
Dim Bitma As New Bitmap(Open.FileName)
Dim imageData As System.Drawing.Imaging.BitmapData
imageData = Bitma.LockBits(New Rectangle(0, 0, Bitma.Width, Bitma.Height), Imaging.ImageLockMode.ReadWrite, Bitma.PixelFormat)
Pic.Image = New Bitmap(Bitma.Width, Bitma.Height, imageData.Stride, Bitma.PixelFormat, New IntPtr(CInt(imageData.Scan0)))
It reads a .jpg image and shows it using a pointer.
I want to do the same to my image. But my problem is that my image is TGA, and VB.net doesn't natively supports it, so i have to read the bytes where the pixel information is, and then create the image. I've tried the setpixel, but it takes like ages.
But thats not really creating a pointer is it? Its just locking the bitmap into the memory. Or... am I wrong?
Re: [2005] Creating a Pointer to a Array of Bytes
ur right! But by looking then into the memory u are then able to get its pointer.
Robertx, for that u would have to have the New sub for the bitmap class, because that is where i want to put the pointer.
Re: [2005] Creating a Pointer to a Array of Bytes
Apologies - I now follow what you are trying to do. I was off in a completely different direction. :afrog:
Re: [2005] Creating a Pointer to a Array of Bytes
lool :P
If u have any idea plz say. Because it seems that there is no way out of this, other then making a C# class, that is what i didnt want.
Re: [2005] Creating a Pointer to a Array of Bytes
I have got no idea whether this will work but it's worth a try:
According to MSDN:
http://msdn2.microsoft.com/en-us/lib...shal.copy.aspx
The System.Runtime.InteropServices.Marshal.Copy Method
"Copies data from a managed array to an unmanaged memory pointer, or from an unmanaged memory pointer to a managed array."
This could be implemented as follows:
Code:
Dim imagedata() As Byte = TGA.GetImageData(TGA.FileBytes)
Dim lpimagedata As IntPtr = Marshal.AllocHGlobal(imagedata.Length)
Marshal.Copy(imagedata, 0, lpimagedata, imagedata.Length)
Pic.Image = New Bitmap(TGA.ImageWidth, TGA.ImageHeight, TGA.ImageStride, Imaging.PixelFormat.Format24bppRgb, lpimagedata)
The third parameter of the Marshal.Copy method ("destination") is a memory pointer (IntPtr) to which data is copied from the first parameter - the byte array containing the image data.
I hope that this somehow points you in the right direction.
Re: [2005] Creating a Pointer to a Array of Bytes
Thks, that helped. Since the image is stored backwards in the file (the end of the image is in the begining of the file), i get the last color. but i dont get the whole image but i dont understand why.