bitmap from stream; how to set size

bwells

Well-known member
Joined
Feb 25, 2003
Messages
84
If I construct a Bitmap object from a Stream object, how do I specify the dimensions of the bitmap? I see one option would be to construct a Bitmap at the desired size, and then use the FromStream method to load the Bitmap object.

Can someone help me to understand how a Bitmap which is constructed from a Stream gets its width and height?

Thanks

Bryan
 
When you make a Bitmap from a stream it already has a width and height and also an image. The only way you can change that is to make a new Bitmap.
 
Here is my confusion.

I have a one dimensional array of byte data I loaded from some audio hardware. I want to transfer that data into a Bitmap object so the data can be represented as an image which is 512x1024.

1) I create a MemoryStream object and call the Write method to transfer my pointer to my byte array into the MemoryStream. So at this point, the MemoryStream is just a stream of bytes and has no dimension, only a length.

2) I call the Bitmap constructor passing in the MemoryStream. Now a Bitmap object has been created, and it still does not have a width or height.

So at what point in this flow does the Bitmap get a width and height?

Another way of doing this would be:

1) construct an Image object using the FromStream method.

2) Construct a bitmap using the Image, and a width and height. Now I understand how the Bitmap has a width and height, but in the first case, there is not any time that I speicify a width and height, yet I still have constructed a Bitmap object.
 
I understand that I "can" make a new bitmap using the constructor that takes a width and height. But what I am asking is if I construct a bitmap using a MemoryStream (not a FileStream), where does the bitmap get its width and height?
 
The stream is just a bunch of data in one of the formats the framework knows about - i.e., it will have its header (be it BMP, JPH, GIF, whatever) which contains data on the dimensions of the image.
 
But in my case, I created the MemoryStream and used the MemoryStream Write method to write an array of data to the Stream. So I did not specify the dimensions of the Bitmap. I am worried that I SHOULD specify these dimensions when I create the MemoryStream. Do I need to write more than the array of data to the stream in order for the header to be correct? The array of data is simply 1024x512 bytes of data in a one dimensional array. I want this to end up in a Bitmap so the Bitmap is 512 wide, and 1024 tall.

thanks
Bryan
 
The data is an image of some audio sampled by custom audio hardware. So in effect, the image is a "raw" image. So I already have some code that write a RAW file from the memory, and then I have a routine that reads the raw file into a Bitmap. But I want to remove the intermediate step of the RAW file and go directly from memory into a Bitmap object for display. I want to show this as an animation that is running through 9FPS, so I want the translation from memory to Bitmap to be as fast as possible.
 
The problem can be summed up with the question: What makes a bitmap?

The answer is that a bitmap contains (within the stream) a header record followed by pixel data. For full details, see:

http://www.wotsit.org/search.asp?s=bmp

When you contruct "a bitmap from a stream", the stream itself already contains the header and pixel - you dont "construct" as such - its already constructed and youre just reading the details from the stream (same as you would from a file).

In this case, you are not supplying a bitmap stream, nor a stream of bitmap data - you are supplying raw binary data (as noted in your most recent comment)

What code are you using to convert from RAW file to a Bitmap? You could probably use very similar code, just without saving to disk first.

But as far as your original statement, "Can someone help me to understand how a Bitmap which is constructed from a Stream gets its width and height?" goes - the stream must already contain all the information required, as it would if reading from a file.

You may be able to do something with Image.RawFormat, but I suspect you already know how to do the conversion (which is the hard bit)
 
You could also try one of the Bitmap constructors (.New), ie:

Public Sub New(ByVal width As Integer, ByVal height As Integer, ByVal stride As Integer, ByVal format As System.Drawing.Imaging.PixelFormat, ByVal scan0 As IntPtr)

Where scan0 is: The address of a stream of pixel data.

Bit confusing terminology, its not a Stream object, rather a location in memory for the pixel data (note that in your case this would already be converted from the RAW data)
 
Thanks for the idea of using this bitmap constructor. I tried this and it seems to be the fastest way to transfer the memory data to a bitmap.
 
Ive been trying to use this constructor for the same purpose (create bitmap obj from raw data). How did you create the pointer to the byte array?
 
Bitmap* Managed_COSP_9054::COSP_9054_VA::getVideoImage( )
{
unsigned char __nogc *buffer =
new unsigned char[ sizeOfUL * imageNumPixels ];

// read 3 bytes per pixel for RGB value
if( !this->m_cosp_va->getVideoBuffer( imageNumPixels,
buffer ))
return 0;

array24bit = new
unsigned char[ sizeOfUL * 3 * imageNumPixels ];

for ( int i = 0; i < sizeOfUL * imageNumPixels; i++ )
{
Byte item = buffer;
array24bit[3*i] = item; // B
array24bit[3*i + 1] = item; // G
array24bit[3*i + 2] = item; // R
}

delete[] buffer;

Bitmap *bmp = new Bitmap( imageWidth, imageHeight,
imageStride, PixelFormat::Format24bppRgb, array24bit );

return bmp
}
 

Similar threads

T
Replies
0
Views
567
This_display_name_is_already_in_use_They_all_are
T
C
Replies
0
Views
142
CompuTechSide
C
Back
Top