473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Int32 value into Bitmap byte pixel???

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?
--
Thanks
Sharon
Mar 28 '07 #1
4 2435
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?
--
Thanks
Sharon
Mar 28 '07 #2
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?
--
Thanks
Sharon
Mar 28 '07 #3
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.

"Sharon" wrote:
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?
--
Thanks
Sharon
Mar 28 '07 #4
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.

"Sharon" wrote:
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?
--
Thanks
Sharon
Mar 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Dean | last post by:
I use the following to read in the decompressed data first. Then when this is all read in i use a method to try to set the relevant pixels to their relevant color. When i output this bitmap its...
8
by: James Dean | last post by:
I have 1 bit per pixel information and i have the width and height of this data.Each bit corresponds to a 24 bit colour value. I want to convert this to 24bit per pixel bitmap. Do i need to...
0
by: James Dean | last post by:
I have trouble displaying a bitmap. I have 1bpp information. I also have a command telling me what color i need to set the bytes to when the colour is switched on. The trouble is i do all this but...
1
by: Dennis Myrén | last post by:
Hi. I use System.Drawing.Bitmap to copy bitmap pixel data. I use LockBits to retrieve a BitmapData which in turn provides me with the Scan0 property which i then use to loop the pixel data using...
1
by: Sharon | last post by:
I have 2 questions that are just the opposite to one another: (1) I need to read an image file (like bitmap, jpeg etc.) and to save only its data, I need to save his data in a raw data format,...
7
by: TomHL | last post by:
hello, - I have a Bitmap Object named: bmp1 - I already have the numerical values of:red,blue and green colors + I have the X and Y cordinates. How do I set the pixel value via safe code on...
2
by: cosminb | last post by:
I have a native function which returns an image in the form of a BYTE* lpDIB array. How do I convert it to a Bitmap, so that I can work with it properly in C#? Thanks in advance, Cosmin
1
by: Sauny | last post by:
Hi all, Ok, I am not the best C programmer in the world but am looking to convert a byte array which contains pixel information to a bitmap. basically i have a screen say 320x280 leading to a...
7
by: RB0135 | last post by:
Hi All, I have some Windows BMP, 1BPP, monochrome files that I need to get the raw data from to load a graphics buffer on a Roll Printer (which I know can be done). Lets forget about the Roll...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.