[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?
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
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:
Public Function ToBitmap() As Bitmap
Dim MyBitmap As New Bitmap(Me.GetImageWidth(FileBytes), Me.GetImageHeight(FileBytes))
Dim Counter As Integer = 18 + Me.GetIDLength(FileBytes) + Me.GetColorMapLength(FileBytes)
For Ycount As Integer = 0 To MyBitmap.Height - 1
For Xcount As Integer = 0 To MyBitmap.Width - 1
If Me.GetImagePixelDepth(FileBytes) = 24 Then
Dim Clr As Color = Color.FromArgb(CInt(FileBytes(Counter)), CInt(FileBytes(Counter + 1)), CInt(FileBytes(Counter + 2)))
MyBitmap.SetPixel(Xcount, Ycount, Clr)
Counter += 3
End If
Next
Next
Return MyBitmap
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.
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?
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.
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?
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.
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))
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?
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?
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.
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.
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
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.