Results 1 to 10 of 10

Thread: [2005] Creating a Pointer to a Array of Bytes

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    [2005] Creating a Pointer to a Array of Bytes

    Hi!!!

    I have this code:
    vb Code:
    1. Dim TGA As New TGAReader(Open.FileName)
    2. Dim imagedata() As Byte = TGA.GetImageData(TGA.FileBytes)
    3. 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??
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Creating a Pointer to a Array of Bytes

    Does VB support pointers like these? I dont think so..C# does though.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] Creating a Pointer to a Array of Bytes

    Watch this code:
    vb Code:
    1. Dim Bitma As New Bitmap(Open.FileName)
    2. Dim imageData As System.Drawing.Imaging.BitmapData
    3. imageData = Bitma.LockBits(New Rectangle(0, 0, Bitma.Width, Bitma.Height), Imaging.ImageLockMode.ReadWrite, Bitma.PixelFormat)
    4. 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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    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.

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Creating a Pointer to a Array of Bytes

    Quote Originally Posted by Lasering
    Watch this code:
    vb Code:
    1. Dim Bitma As New Bitmap(Open.FileName)
    2. Dim imageData As System.Drawing.Imaging.BitmapData
    3. imageData = Bitma.LockBits(New Rectangle(0, 0, Bitma.Width, Bitma.Height), Imaging.ImageLockMode.ReadWrite, Bitma.PixelFormat)
    4. 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?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  7. #7
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    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.

  8. #8

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    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.
    Last edited by robertx; Dec 1st, 2007 at 09:48 AM.

  10. #10

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Last edited by Lasering; Dec 9th, 2007 at 06:10 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width