Int32 value into Bitmap byte pixel???

G

Guest

I have an RGB color held in a Int32 variable.

I using an unsafe code to loop through Bitmap data, and by using the
BitmapData.Scan0 pointer I need to copy the Int32 color value into the Bitmap
data.
But using the:
/////////////////////////////////////////////////////////////////////////////////////////
Bitmap img = new Bitmap(100, 200,
System.Drawing.Imaging.PixelFormat.Format32bppRgb);
System.Drawing.Imaging.BitmapData refData = img.LockBits(new Rectangle(0, 0,
img.Width, img.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly,
img.PixelFormat);

int myColot = GetColor(); // The RGB color value in a Int32 variable.

unsafe
{
refBuf = (byte*)refData.Scan0.ToPointer();

*refBuf = myColot; // This is the problematic code line !!!
}
/////////////////////////////////////////////////////////////////////////////////////////

How do I need to change the problematic code line above, to make it work?
 
G

Guest

Thanks Daniel,

I assume that the value is of type integer. If so; the problem remains when
we sum a byte value (like mainPointer[kBlue]) with int value and then casting
it back to byte.
the int result will be cut to a byte and will result with a loss of data.

BTY: What are the kBlue, kGreen, kRed?
 
G

Guest

I'm afraid that I'm still don't understand something.

Something like that for green: (byte)(mainPointer[1] + value) when value
is an Int32 and mainPointer[0] is a byte, will result that the blue part of
the value will be summed with the mainPointer[0].
I'm I wrong?

I would expect it to be like that (where colorInt32 in the Int32 with the
RGB color value):
mainPointer[0] = (byte)(colorInt32 * 0xF);
mainPointer[0] = (byte)((colorInt32 >> 8 ) * 0xF);
mainPointer[0] = (byte)((colorInt32 >> 16) * 0xF);
mainPointer += 3;

What I'm getting wrong?
 
G

Guest

Ok. The thing is that your getting an RGB value to an integer. That is making
something like: int myColor = redColor | greenColor << 8 | blueColor << 16
and then *refBuf = myColot

First of all your trying to assign a int value to a byte value that may fall
in a loss of data. Bisides that, refBuf is a pointer to an array of bytes
which are grouped by four, or in my example, by 3 and represent each pixel in
the image.
That is why I did

mainPointer[0] = (byte)(mainPointer[0] + value); // blue
mainPointer[1] = (byte)(mainPointer[1] + value); // green
mainPointer[2] = (byte)(mainPointer[2] + value); // red
mainPointer += 3;

instead of having the RGB value on a integer you must have them separated.

you assign them separately and then move the pointer to the next valid group
representing the next valid pixel.

kBlue, kGreen and kRed are constants...

Bye.
 
G

Guest

Here you're assigning just the blue color:

// allways blue
mainPointer[0] = (byte)(colorInt32 * 0xF);
mainPointer[0] = (byte)((colorInt32 >> 8 ) * 0xF);
mainPointer[0] = (byte)((colorInt32 >> 16) * 0xF);

// blue, green, red
mainPointer[0] = (byte)(colorInt32 * 0xF);
mainPointer[1] = (byte)((colorInt32 >> 8 ) * 0xF);
mainPointer[2] = (byte)((colorInt32 >> 16) * 0xF);

check this http://www.geocities.com/da_tes/Alix.zip
Alix is a very little app I was working on... check Alix.Core.ImageHolder
there you have 2 example methods for image manipulation: SetBrightness and
SetContrast
they do what you're actually trying to do.

bye.
 

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

Similar Threads


Top