Results 1 to 14 of 14

Thread: [2005] Create a Image having a image data in a byte array

  1. #1

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

    [2005] Create a Image having a image data in a byte array

    Hi!!

    I have a byte array containing pixel information, there are 3 bytes for each pixel
    (Red, Green , Blue). And I want to create a image with that byte array.
    I already have a code that does that but is extremely slow, only after about 5 secs he shows the image and the image is only 300x300 pixels. The code loops throw the image height and then throw the image width and sets the pixel color using the values in the array.
    But i think that there is a simplest and much more faster way to do that. Is there?
    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
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] Create a Image having a image data in a byte array

    Without seeing your code, I am not quite sure what your problem is.

    Give this a try. This procedure converts a byte array into an Image object:

    Code:
        Public Function ByteArrayToImage(ByVal myByteArray() As Byte) As Image
            Dim myStream As New IO.MemoryStream
            Dim myImage As Image
    
            myStream.Write(myByteArray, 0, myByteArray.GetUpperBound(0))
            myImage = Image.FromStream(myStream)
            Return myImage
    
        End Function

  3. #3

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

    Re: [2005] Create a Image having a image data in a byte array

    I get an error saying that the Parameter is not valid in the line Image.FromStream(myStream)

    Here is my code:
    vb Code:
    1. Public Function ToBitmap() As Bitmap
    2.         Dim MyBitmap As New Bitmap(Me.GetImageWidth(FileBytes), Me.GetImageHeight(FileBytes))
    3.         Dim Counter As Integer = 18 + Me.GetIDLength(FileBytes) + Me.GetColorMapLength(FileBytes)
    4.         For Ycount As Integer = 0 To MyBitmap.Height - 1
    5.             For Xcount As Integer = 0 To MyBitmap.Width - 1
    6.                 If Me.GetImagePixelDepth(FileBytes) = 24 Then
    7.                     Dim Clr As Color = Color.FromArgb(CInt(FileBytes(Counter)), CInt(FileBytes(Counter + 1)), CInt(FileBytes(Counter + 2)))
    8.                     MyBitmap.SetPixel(Xcount, Ycount, Clr)
    9.                     Counter += 3
    10.                 End If
    11.             Next
    12.         Next
    13.         Return MyBitmap
    14.     End Function
    FileBytes is an array of bytes, the rest methods are pretty simple. If u need the code of them i can give you.
    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
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Create a Image having a image data in a byte array

    I don't see the line, "Image.FromStream(myStream)," in that code. Perhaps I am missing something?

  5. #5

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

    Re: [2005] Create a Image having a image data in a byte array

    Well. I've tried robertx code and in the line "Image.FromStream(myStream)" i got that error. But he asked me for my initial code so I posted it.
    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."

  6. #6
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Create a Image having a image data in a byte array

    Are you sure that you are inputting valid data as the arguement to the function?

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

    Re: [2005] Create a Image having a image data in a byte array

    I was able to replicate this error by creating a byte array from an executable file then passing it to the ByteArrayToImage function so I think that Fromethius is on the money. It would appear that you are passing invalid data to the function - a byte array which cannot be converted into an image object.

  8. #8
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: [2005] Create a Image having a image data in a byte array

    I imagine there would be header information your array doesn't have, you should observe a bitmap file for reference.

    Or, construct an image object, loop through your byte array and on your bitmap object (bit for example) bit.setPixel(0,0,color.fromargb(your 3 bytes for this pixel go here))

  9. #9
    Addicted Member
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    154

    Re: [2005] Create a Image having a image data in a byte array

    Why do you wan't to make an image with pixels? Is it maybe to make a program such a paint?

  10. #10

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

    Re: [2005] Create a Image having a image data in a byte array

    Well sorry for not answer you, because I went out all th weekend.
    The byte array that i pass is from a TGA image file, i only pass the image information, that is 3 bytes for each pixel and each byte has the intensities for RGB. Phill64 that is the code that i had in the begining and i've posted it.
    Kevin_hash: can images be done in other way then pixels?
    Fromethius: How should the byte array be? Doesnt it need a header?
    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."

  11. #11
    Addicted Member
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    154

    Re: [2005] Create a Image having a image data in a byte array

    sorry I meant that why don't can't you make a program with an image program like photoshop. but maybe you just want to make such a program yourself.

  12. #12

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

    Re: [2005] Create a Image having a image data in a byte array

    I can open TGAs in Photoshop, but the idea is to be able to open them in vb.
    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."

  13. #13
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: [2005] Create a Image having a image data in a byte array

    VB .net's setpixel is very slow. I did some work a while back and found this example. The base is in c#, but you can reference a compiled dll in VB and use its fuctionality.

    http://www.codeproject.com/cs/media/ImageTraverser.asp
    Visual Studio .NET 2005/.NET Framework 2.0

  14. #14

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

    Re: [2005] Create a Image having a image data in a byte array

    Well the source code is very helpful and not. Why? because I had already a dll that loads "me" the TGA files very fast and good. But the program I've made is a very simple tool and it has 2 files the exe and the dll. So I went into create a class my self to load the TGA, so that i didn't have to use the dll.
    With that code i can do almost everything, except the code that works with the pointers, because VB sucks at pointers. I know i have to use the IntPtr class but yet i still get errors:
    like i had the code in C# +/- like this IntPtr.Zero.ToPointer, but in VB .ToPointer "doesn't exist", how can I overcome this?
    And how can i do a "thing" like this Private baseImagePtr As Integer*
    because i really dont understand C# (I used a converter), and that line in VB i think would get very diferent. I only know that the * is so that u can work with pointers but that doenst 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."

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