Stimphy
asked on
Converting Byte Array To Bitmap
Hi,
I have a byte array (points only) that I need to convert to a bitmap.
When I try :
dim bImage((320*240)-1) as Byte
Dim img As New Bitmap(New System.IO.MemoryStream(bIm age))
it errors when the bitmap is created.
I can do it by looping through all the x,y's, but it is too slow. I need a faster method.
I am developing in VB.NET 2003.
I would like to see an example in VB.NET on how this is performed.
Thanks!
I have a byte array (points only) that I need to convert to a bitmap.
When I try :
dim bImage((320*240)-1) as Byte
Dim img As New Bitmap(New System.IO.MemoryStream(bIm
it errors when the bitmap is created.
I can do it by looping through all the x,y's, but it is too slow. I need a faster method.
I am developing in VB.NET 2003.
I would like to see an example in VB.NET on how this is performed.
Thanks!
Hi,
This way might be easier to read &2 apply ... ;-))
Dim stream As MemoryStream = New MemoryStream
Dim bitmapBytes As Byte()
'Create bitmap
Dim bitmap As Bitmap = New Bitmap(50, 50)
bitmap.Save(stream, System.Drawing.Imaging.Ima geFormat.J peg)
bitmapBytes = stream.ToArray
stream.Close()
bitmap.Dispose()
Best regards,
Raisor
This way might be easier to read &2 apply ... ;-))
Dim stream As MemoryStream = New MemoryStream
Dim bitmapBytes As Byte()
'Create bitmap
Dim bitmap As Bitmap = New Bitmap(50, 50)
bitmap.Save(stream, System.Drawing.Imaging.Ima
bitmapBytes = stream.ToArray
stream.Close()
bitmap.Dispose()
Best regards,
Raisor
ASKER
My image is grayscale.
When I try your code applied to my needs as :
'Temporary Test
Dim bImage(76799) as byte
Dim Cnt as Long
For Cnt = 0 to 76766
bImage(Cnt)=255
Next Cnt
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(bIm age)
Dim img As New Bitmap(ImgWidth, ImgHeight)
img.Save(stream, System.Drawing.Imaging.Ima geFormat.J peg)
img = New Bitmap(stream)
PictureBox1.Image=img
stream.Close()
img.Dispose()
stream = Nothing
img = Nothing
I get a black image....
When I try your code applied to my needs as :
'Temporary Test
Dim bImage(76799) as byte
Dim Cnt as Long
For Cnt = 0 to 76766
bImage(Cnt)=255
Next Cnt
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(bIm
Dim img As New Bitmap(ImgWidth, ImgHeight)
img.Save(stream, System.Drawing.Imaging.Ima
img = New Bitmap(stream)
PictureBox1.Image=img
stream.Close()
img.Dispose()
stream = Nothing
img = Nothing
I get a black image....
ASKER
Ok maybe I'm not being that clear as to what my intentions are... this is what i am doing now to generate my bitmap from an array of bytes.
Dim img As New Bitmap(ImgWidth, ImgHeight)
pixCnt = 0
For y = 0 To ImgHeight - 1
For x = 0 To ImgWidth - 1
If (pixCnt) > bImage.GetUpperBound(0) Then
Exit For
End If
'If x = 319 Then Stop
img.SetPixel(x, y, System.Drawing.Color.FromA rgb(bImage (pixCnt), bImage(pixCnt), bImage(pixCnt)))
pixCnt += 1
Next
Next
I would like a more effecient method than what is being done here....
Hope thats alittle more clear
Thanks
Dim img As New Bitmap(ImgWidth, ImgHeight)
pixCnt = 0
For y = 0 To ImgHeight - 1
For x = 0 To ImgWidth - 1
If (pixCnt) > bImage.GetUpperBound(0) Then
Exit For
End If
'If x = 319 Then Stop
img.SetPixel(x, y, System.Drawing.Color.FromA
pixCnt += 1
Next
Next
I would like a more effecient method than what is being done here....
Hope thats alittle more clear
Thanks
Hi,
I've added PictureBox1 and Button1 to Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = AddPic(320, 240)
End Sub
Private Function AddPic(ByVal ImgWidth As Int32, ByVal ImgHeight As Int32) As Image
Dim bmp As Bitmap = New Bitmap(ImgWidth, ImgHeight)
Dim pxCnt As Int32, currentColor As Color
Dim imageBytes() As Byte = Nothing
For ImgWidth = 0 To 320 - 1
For ImgHeight = 0 To 240 - 1
If pxCnt <= 255 Then
currentColor = System.Drawing.Color.FromA rgb(pxCnt, pxCnt, pxCnt)
Else
pxCnt = 0
End If
pxCnt += 1
bmp.SetPixel(ImgWidth, ImgHeight, currentColor)
Next ImgHeight
Next ImgWidth
Return bmp
End Function
... this gives a gray scale <- trying the same thing with an Array in 0-Dimension will set color continuly to black -> same with passing into a MemoryStream before ...
... when I tested the upper code it wasn't slow at all!
Best regards,
Raisor
I've added PictureBox1 and Button1 to Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = AddPic(320, 240)
End Sub
Private Function AddPic(ByVal ImgWidth As Int32, ByVal ImgHeight As Int32) As Image
Dim bmp As Bitmap = New Bitmap(ImgWidth, ImgHeight)
Dim pxCnt As Int32, currentColor As Color
Dim imageBytes() As Byte = Nothing
For ImgWidth = 0 To 320 - 1
For ImgHeight = 0 To 240 - 1
If pxCnt <= 255 Then
currentColor = System.Drawing.Color.FromA
Else
pxCnt = 0
End If
pxCnt += 1
bmp.SetPixel(ImgWidth, ImgHeight, currentColor)
Next ImgHeight
Next ImgWidth
Return bmp
End Function
... this gives a gray scale <- trying the same thing with an Array in 0-Dimension will set color continuly to black -> same with passing into a MemoryStream before ...
... when I tested the upper code it wasn't slow at all!
Best regards,
Raisor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Raisor,
I am processing the image data coming in from a CCD on a PDA. Trust me, it is pretty slow on a PDA :) about 3-7 seconds behind the current image.
Athough, I have found my solution after scowering the web with Google, and piecing serveral examples together. I will award the points to you because of your efforts to help.
Best Regaurds,
Stimphy
I am processing the image data coming in from a CCD on a PDA. Trust me, it is pretty slow on a PDA :) about 3-7 seconds behind the current image.
Athough, I have found my solution after scowering the web with Google, and piecing serveral examples together. I will award the points to you because of your efforts to help.
Best Regaurds,
Stimphy
You may test this sample and apply it to your project:
Dim stream As MemoryStream = New MemoryStream Dim bitmapBytes As Byte()
'Create bitmap Dim bitmap As Bitmap = New Bitmap(50, 50)
bitmap.Save(stream, System.Drawing.Imaging.Ima
stream.Close() bitmap.Dispose()
Best regards,
Raisor