Drawing in a picturebox

R

Ringo

I'm new to C# and want a simple app to display a bitmap that is in
bayer format. I searched the group and have an app that can draw pixels
in a picturebox using Pset. My problem is that Pset uses x,y,color,
where color is predefined. I need to draw my pixel based on the value
in my file. So if I get a value for a blue pixel of 50, how do I make
that a darker blue than a value of 100? I know Pset is probably not the
best way to do it, but I'm a newbie and it is easy to use.

Here is my code so far;

for (y = 0; y < 144; y++)
{
for (x = 0; x < 176; x+=4)
{
ByrArray[x, y] = br.ReadByte();
picture.SetPixel(x, y, Color.Red);
picture.SetPixel(x+1, y, Color.Green);
picture.SetPixel(x+2, y, Color.Blue);
picture.SetPixel(x+3, y, Color.Green);
}
}

is there a way to scale the colors?
Thanks
Ringo
 
B

Bruce Wood

Ringo said:
I'm new to C# and want a simple app to display a bitmap that is in
bayer format. I searched the group and have an app that can draw pixels
in a picturebox using Pset. My problem is that Pset uses x,y,color,
where color is predefined. I need to draw my pixel based on the value
in my file. So if I get a value for a blue pixel of 50, how do I make
that a darker blue than a value of 100? I know Pset is probably not the
best way to do it, but I'm a newbie and it is easy to use.

Here is my code so far;

for (y = 0; y < 144; y++)
{
for (x = 0; x < 176; x+=4)
{
ByrArray[x, y] = br.ReadByte();
picture.SetPixel(x, y, Color.Red);
picture.SetPixel(x+1, y, Color.Green);
picture.SetPixel(x+2, y, Color.Blue);
picture.SetPixel(x+3, y, Color.Green);
}
}

is there a way to scale the colors?

I think that you have made an incorrect assumption about the Color
class. Just because there are some predefined colours, doesn't mean
you're limited to only those colours. Take a look at the constructor:

Color mySpecialColour = new Color(redValue, greenValue, blueValue);

where each of the three values can be from 0 to 255. I believe that
there is also an (easy) way to get a Color based on cmyk values.
 
R

Ringo

Bruce said:
Ringo said:
I'm new to C# and want a simple app to display a bitmap that is in
bayer format. I searched the group and have an app that can draw pixels
in a picturebox using Pset. My problem is that Pset uses x,y,color,
where color is predefined. I need to draw my pixel based on the value
in my file. So if I get a value for a blue pixel of 50, how do I make
that a darker blue than a value of 100? I know Pset is probably not the
best way to do it, but I'm a newbie and it is easy to use.

Here is my code so far;

for (y = 0; y < 144; y++)
{
for (x = 0; x < 176; x+=4)
{
ByrArray[x, y] = br.ReadByte();
picture.SetPixel(x, y, Color.Red);
picture.SetPixel(x+1, y, Color.Green);
picture.SetPixel(x+2, y, Color.Blue);
picture.SetPixel(x+3, y, Color.Green);
}
}

is there a way to scale the colors?

I think that you have made an incorrect assumption about the Color
class. Just because there are some predefined colours, doesn't mean
you're limited to only those colours. Take a look at the constructor:

Color mySpecialColour = new Color(redValue, greenValue, blueValue);

where each of the three values can be from 0 to 255. I believe that
there is also an (easy) way to get a Color based on cmyk values.


Thanks, so how do I define a new color on the fly?
is there a way to do somethign like picture.SetPixel(x, y, (R,G,B));
I tried adding
Color mySpecialColour = new Color(255,0, 0);
to my program just to see if it would make red but it complains about
not having a constructor.
The type 'System.Drawing.Color' has no constructors defined
How/where do I add the constructor, or is this not what I need to do.
 
T

Torben K. Jensen

Ringo said:
Thanks, so how do I define a new color on the fly?
is there a way to do somethign like picture.SetPixel(x, y, (R,G,B));
I tried adding
Color mySpecialColour = new Color(255,0, 0);
to my program just to see if it would make red but it complains about
not having a constructor.
The type 'System.Drawing.Color' has no constructors defined
How/where do I add the constructor, or is this not what I need to do.

If you look in the .NET documentation, youøll find a FromArgb() method, which will serve your purpose.

There are two options for you, depending on whether your original image has a transparency (alpha) channel or not:

public static Color FromArgb (
int red,
int green,
int blue
)

or

public static Color FromArgb (
int alpha,
int red,
int green,
int blue
)

Use them like this:

picture.SetPixel(x, y, Color.FromArgb(R, G, B));

Hope that helps.
 
B

Bruce Wood

Use them like this:
picture.SetPixel(x, y, Color.FromArgb(R, G, B));

Yes. Sorry. Color (oddly) doesn't have public constructors. Instead it
has the static FromArgb methods. My mistake.
 
R

Ringo

Cool, That is what I needed. Thanks
Bruce said:
Yes. Sorry. Color (oddly) doesn't have public constructors. Instead it
has the static FromArgb methods. My mistake.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top