Question How to make a bitmap from memory

keb1965

Well-known member
Joined
Feb 9, 2009
Messages
103
Programming Experience
10+
I have a bitmap that is being extracted piecemeal from another file where it has been stored. I can successfully read and iterpret the data and write it out to a file that can be edited/viewed/previewed etc. However, this is not the direction I would like to go.

I already have the entire file in a char() buffer that I write out to a file, but I would prefer to simply put that information directly into a PictureBox or other image control.

VB.NET:
    Private Shared Sub bmpWriteFile(ByVal FileName As String, _
         ByRef charHeader As Byte(), _
         ByRef charBuff As Byte())

        Dim fs As IO.FileStream = New IO.FileStream(FileName, IO.FileMode.Create, IO.FileAccess.Write)
        Dim bw As IO.BinaryWriter = New IO.BinaryWriter(fs)
        bw.Seek(0, IO.SeekOrigin.Begin)
        bw.Write(charHeader)
        bw.Write(charBuff)
        bw.Close()
        fs.Close()
    End Sub

Just an FYI, I have to create the header on the fly since it is stripped from the bitmap in the source file .. that is why I write it seperately.

What I am currently doing is saving the file, then reading it into a new bitmap and assigning it to the control. If I could get away from the saving to file step it would be great!

VB.NET:
        bmpWriteFile("C:\Temp.bmp", charHEADERBuffer, charBMPBuffer)
        Dim bmp As New Bitmap("C:\Temp.bmp")
        Me.PictureBox1.Image = bmp

Any help would be appreciated.
 
Nevermind ..... I have figured out a workaround

VB.NET:
Dim bmp As New Bitmap(bmpHeader.biWidth, bmpHeader.biHeight)
...
'loop through each pixel in the char buffer getting each color
'and put it in the pixel where it belongs
bmp.SetPixel(locX, locY, color)
 
A lot faster should be to load the "file" bytes into a MemoryStream and load Image.FromStream.
 
nope .. tried it and it didn't work ... invalid format .. the byte stream is not a complete bitmap ... it is merely the pixel data, no color palette and no header information ... I suppose I could put the header, pallette and pixel data into a single stream, but then I would just have to add more overhead.

Besides, it is fast enough anyway, considering the largest bitmap it will ever have to deal with is 200x200 w/256 colors
 
By "file" bytes I meant the same stuff you put in the file and loaded to Bitmap, it's no different than that, only saving the disk operations.
 
Ah ... I understand ... the problem is I would have 2 different streams, since the bitmap header doesn't exist in the data read from the source. I would have to recreate it and put it into the stream from the data source ...

As it is now, I only have the width and height information and the pixel data .. the source doesn't maintain the 256 color palette or the 40 byte header .. so I would have to create the header data and color palette and then inject that into the byte array of pixel data ... when I was writing it to file, I merely created a default header with the standard 256 color palette and wrote it before the pixel data.
 
Back
Top