Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Converting Byte Array To Bitmap

Posted on 2005-03-15
7
Medium Priority
?
465 Views
Last Modified: 2010-08-05
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(bImage))

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!
0
Comment
Question by:Stimphy
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 13549429
Hi,

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.ImageFormat.Jpeg)  bitmapBytes = stream.ToArray
  stream.Close()  bitmap.Dispose()


Best regards,
Raisor
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 13549455
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.ImageFormat.Jpeg)  
  bitmapBytes = stream.ToArray
  stream.Close()  
  bitmap.Dispose()


Best regards,
Raisor
0
 
LVL 3

Author Comment

by:Stimphy
ID: 13550138
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(bImage)
Dim img As New Bitmap(ImgWidth, ImgHeight)
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
img = New Bitmap(stream)
PictureBox1.Image=img

stream.Close()
img.Dispose()
stream = Nothing
img = Nothing

I get a black image....
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Author Comment

by:Stimphy
ID: 13550934
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.FromArgb(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
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 13552914
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.FromArgb(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
0
 
LVL 15

Accepted Solution

by:
Ralf Klatt earned 2000 total points
ID: 13552927
Hi,

The line "Dim imageBytes() As Byte = Nothing" can be deleted <- I'd used it for testing arround with an Array and then just forgot to delete it ;-)


Best regards,
Raisor
0
 
LVL 3

Author Comment

by:Stimphy
ID: 13553023
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question