Click here to Skip to main content
15,867,292 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
VS 2010
Visual Basic

Sorry for strange subject line!

I have a need to do additive/subtractive operations on a 2D array of Bytes (GIF). I cannot get a Byte() to an Image, though. Many methods out there do not work, because they are asking FromStream to cast from MemoryStream() to Stream(), which it can't/won't do. For example:

imgOnFOrm.Image = Image.FromStream(memStream)


Will throw an exception that literally hundreds of people are stuck on, where the method FromStream complains

Exception in yourMethod: System.ArgumentException: Parameter is not valid.


It appears that in earlier version of VS, this was allowed, as I see many examples that people say works. Strange.

So I create a random series of Bytes, but can find no way (without writing to the file system) to make it show up on my form's PictureBox component.

Objective (Big Picture)

I need to store data for calculations in 2D arrays, adding and subtracting other 2D arrays to/from it. In order to check the charts & values, I also need to see this data.

Now the good news is that I only need gray scale. Each pixel will vary in shading from 0 to 255, and gray is just fine. I don't need other colors, although the thought of doing 3 colors is intriguing-as long as I could break out each color, which seems realistic.

MS Chart might even be a solution, but I haven't tried. I thought the simpler approach of manipulating Bytes would be easier, but I am missing the obvious, apparently.

Any thoughts?

TIA!

pat
:)
Posted

Hi,

Use Bitmap instead of image. Bit map can be directly created from image file name or from image itself

Dim bmp as Bitmap=new Bitmap(image);

then you may write it to a memory stream and get the bytes. Refer

http://www.vbforums.com/showthread.php?t=358917[^]

But this will give you all the image bytes, I mean all the bytes the file has including headers etc.,

If you only want the pixel values then loop through it

C#
int xBound = bmp.Width;
  int yBound = bmp.Height;


The c# code here. Convert to VB.net

C#
Byte[,] pixels=new Byte[xBound,yBound];
            Color clr;
            for (int i = 0; i < xBound - 1; i++)
            {
                for (int j = 0; j < xBound - 1; j++)
                {
                    clr = bitmap.GetPixel(i, j);
                    pixels[i, j] = clr.R;
                }
            }


Note: for a grey scale clr.R,clr.G, clr.B all are results in same values.

After the 2D array manipulations you may use setPixels to the bitmap.

The bitmap can be set to a picturebox

picturebox1.Image=bitmap;


If you want ...

So I create a random series of Bytes, but can find no way (without writing to the file system) to make it show up on my form's PictureBox component.
then

C#
Bitmap bitmap2 = new Bitmap(xBound, yBound);
for (int i = 0; i < xBound - 1; i++)
{
    for (int j = 0; j < xBound - 1; j++)
    {
        bitmap2.SetPixel(i,j,Color.FromArgb(Random value,random value,random value));
    }
}
pictureBox1.Image = bitmap2;


or generate bitmap from a stream. There are 12 overloaded methods to create the bitmap. Your choice
 
Share this answer
 
v3
Comments
Sandeep Mewara 13-Feb-11 2:19am    
Good answer. 5!
VB
Private Sub makeBMP()
    Dim bmp As New Bitmap(100, 100)
    Dim xBound As Int16 = bmp.Width
    Dim yBound As Int16 = bmp.Height
    Dim generator As New Random

    For x = 0 To xBound - 1
        For y = 0 To yBound - 1
            bmp.SetPixel(x, y, Color.FromArgb(generator.Next(0, 256), generator.Next(0, 256), generator.Next(0, 256)))
        Next
    Next
    PictureBox1.Image = bmp
End Sub


@AlbinAbel, thank you so very much for your example. The above is a complete, working sub based on your advice.

True, I could make it ask for the bounds in the constructor, but this works great.

Like I had said, I need the 2D aspect (instead of just putting a program's or a file's bytes) in order to show an actual plot of values. The reason is that I need to add and subtract other plots from it. I really only need one RGB channel for this, and was ready to just use grayscale, but having two other colors makes me think I can make good use of them by plotting other data-perhaps even the last plot that was merged/added-at the same time showing the result of the addition/subtraction/merge.

This also runs super fast, which is something not talked about in my ref materials when it comes to graphics, bitmaps, and images in general.

Anyway, just wanted to explain, and give back to the group a working, self-contained example for others.

Thanks again!

pat
:)
 
Share this answer
 
v2
Comments
Albin Abel 13-Feb-11 15:37pm    
You are most welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900