Int32 value into Bitmap byte pixel???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U2hhcm9u?=

    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.Scan 0 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.PixelFo rmat.Format32bp pRgb);
    System.Drawing. Imaging.BitmapD ata refData = img.LockBits(ne w Rectangle(0, 0,
    img.Width, img.Height), System.Drawing. Imaging.ImageLo ckMode.WriteOnl y,
    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
  • =?Utf-8?B?U2hhcm9u?=

    #2
    RE: Int32 value into Bitmap byte pixel???

    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

    Comment

    • =?Utf-8?B?U2hhcm9u?=

      #3
      RE: Int32 value into Bitmap byte pixel???

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

      Something like that for green: (byte)(mainPoin ter[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)(colorInt 32 * 0xF);
      mainPointer[0] = (byte)((colorIn t32 >8 ) * 0xF);
      mainPointer[0] = (byte)((colorIn t32 >16) * 0xF);
      mainPointer += 3;

      What I'm getting wrong?


      --
      Thanks
      Sharon

      Comment

      • =?Utf-8?B?RGFuaWVsIEUuIFRlc3Rh?=

        #4
        RE: Int32 value into Bitmap byte pixel???

        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)(mainPoin ter[0] + value); // blue
        mainPointer[1] = (byte)(mainPoin ter[1] + value); // green
        mainPointer[2] = (byte)(mainPoin ter[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

        Comment

        • =?Utf-8?B?RGFuaWVsIEUuIFRlc3Rh?=

          #5
          RE: Int32 value into Bitmap byte pixel???

          Here you're assigning just the blue color:

          // allways blue
          mainPointer[0] = (byte)(colorInt 32 * 0xF);
          mainPointer[0] = (byte)((colorIn t32 >8 ) * 0xF);
          mainPointer[0] = (byte)((colorIn t32 >16) * 0xF);

          // blue, green, red
          mainPointer[0] = (byte)(colorInt 32 * 0xF);
          mainPointer[1] = (byte)((colorIn t32 >8 ) * 0xF);
          mainPointer[2] = (byte)((colorIn t32 >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.Image Holder
          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)(mainPoin ter[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)(colorInt 32 * 0xF);
          mainPointer[0] = (byte)((colorIn t32 >8 ) * 0xF);
          mainPointer[0] = (byte)((colorIn t32 >16) * 0xF);
          mainPointer += 3;
          >
          What I'm getting wrong?
          >
          >
          --
          Thanks
          Sharon

          Comment

          Working...