?
Solved

Converting Byte Array To Bitmap

Posted on 2005-03-15
7
Medium Priority
?
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

771 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