Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. How to AddAlpha Values ??

How to AddAlpha Values ??

Scheduled Pinned Locked Moved Graphics
questiontutorial
6 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SandipG
    wrote on last edited by
    #1

    How can i change alpha channel values without rinning Height*width loop Is there any way that i can drawimage without altering alpha values of background??. i mean i have two images where bye sequences of pixels will be as follows 22 0 34 0 56 0 255 14 55 0 22 0 44 0 255 10 23 0 37 0 56 0 255 14 51 0 28 0 44 0 255 10 now i want to replace 255 with other values or Replace 0's with values from first column.

    M 1 Reply Last reply
    0
    • S SandipG

      How can i change alpha channel values without rinning Height*width loop Is there any way that i can drawimage without altering alpha values of background??. i mean i have two images where bye sequences of pixels will be as follows 22 0 34 0 56 0 255 14 55 0 22 0 44 0 255 10 23 0 37 0 56 0 255 14 51 0 28 0 44 0 255 10 now i want to replace 255 with other values or Replace 0's with values from first column.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I'm confused. How does a drawimage alter alpha values of a background?

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        I'm confused. How does a drawimage alter alpha values of a background?

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        S Offline
        S Offline
        SandipG
        wrote on last edited by
        #3

        If i create the graphics object from a bitmap object which has some alpha values. Now using this graphics object if i draw some other bitmap on firstbitmap the alpha values are altered i mean they will be set to 255.

        M 1 Reply Last reply
        0
        • S SandipG

          If i create the graphics object from a bitmap object which has some alpha values. Now using this graphics object if i draw some other bitmap on firstbitmap the alpha values are altered i mean they will be set to 255.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Gotcha. Since there's only two compositing modes, you may have to loop through and combine the alpha values the way you want to.

          SandipG wrote:

          now i want to replace 255 with other values or Replace 0's with values from first column.

          CompositingModeSourceOver works for the "Replace 0's with values from first column" case but not for the "replace 255 with other values" case. Here's some code you can play with to see the compositing results...

          // Create a 20x20 ARGB bitmap
          Gdiplus::Bitmap Bitmap1(20, 20, PixelFormat32bppARGB);

          Gdiplus::Rect rect(0, 0, 20, 20);

          // Create a Graphics object from the bitmap
          Gdiplus::Graphics *pBitmapGraphics1 = Gdiplus::Graphics::FromImage(&Bitmap1);

          // Try different compositing modes
          //pBitmapGraphics1->SetCompositingMode(Gdiplus::CompositingModeSourceCopy);
          pBitmapGraphics1->SetCompositingMode(Gdiplus::CompositingModeSourceOver);

          // Try different colors to blend - brush is background, pen is foreground
          Gdiplus::SolidBrush solidbrush(Gdiplus::Color(0xFF,0x00,0xFF,0x00));//Gdiplus::Color::Transparent);
          Gdiplus::Pen solidpen(Gdiplus::Color(0x40,0xFF,0x00,0x00), 1.0);//Gdiplus::Color::Transparent, 1.0);

          // Fill the background with the brush color and draw the first row with the pen, using
          // the current compositing mode
          pBitmapGraphics1->FillRectangle(&solidbrush, rect);
          pBitmapGraphics1->DrawLine(&solidpen, 0, 0, 19, 0);

          // Release the bitmap from the graphics object so we can look at the pixel bits
          delete pBitmapGraphics1;
          pBitmapGraphics1 = 0;

          // Look at the pixel bits (look at pRGBQuads in a debugger memory window)
          Gdiplus::BitmapData bitmapData;
          Bitmap1.LockBits(&rect, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
          RGBQUAD *pRGBQuads = (RGBQUAD *)bitmapData.Scan0;
          Bitmap1.UnlockBits(&bitmapData); //<-- put breakpoint here

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          S 1 Reply Last reply
          0
          • M Mark Salsbery

            Gotcha. Since there's only two compositing modes, you may have to loop through and combine the alpha values the way you want to.

            SandipG wrote:

            now i want to replace 255 with other values or Replace 0's with values from first column.

            CompositingModeSourceOver works for the "Replace 0's with values from first column" case but not for the "replace 255 with other values" case. Here's some code you can play with to see the compositing results...

            // Create a 20x20 ARGB bitmap
            Gdiplus::Bitmap Bitmap1(20, 20, PixelFormat32bppARGB);

            Gdiplus::Rect rect(0, 0, 20, 20);

            // Create a Graphics object from the bitmap
            Gdiplus::Graphics *pBitmapGraphics1 = Gdiplus::Graphics::FromImage(&Bitmap1);

            // Try different compositing modes
            //pBitmapGraphics1->SetCompositingMode(Gdiplus::CompositingModeSourceCopy);
            pBitmapGraphics1->SetCompositingMode(Gdiplus::CompositingModeSourceOver);

            // Try different colors to blend - brush is background, pen is foreground
            Gdiplus::SolidBrush solidbrush(Gdiplus::Color(0xFF,0x00,0xFF,0x00));//Gdiplus::Color::Transparent);
            Gdiplus::Pen solidpen(Gdiplus::Color(0x40,0xFF,0x00,0x00), 1.0);//Gdiplus::Color::Transparent, 1.0);

            // Fill the background with the brush color and draw the first row with the pen, using
            // the current compositing mode
            pBitmapGraphics1->FillRectangle(&solidbrush, rect);
            pBitmapGraphics1->DrawLine(&solidpen, 0, 0, 19, 0);

            // Release the bitmap from the graphics object so we can look at the pixel bits
            delete pBitmapGraphics1;
            pBitmapGraphics1 = 0;

            // Look at the pixel bits (look at pRGBQuads in a debugger memory window)
            Gdiplus::BitmapData bitmapData;
            Bitmap1.LockBits(&rect, ImageLockModeRead, PixelFormat32bppARGB, &bitmapData);
            RGBQUAD *pRGBQuads = (RGBQUAD *)bitmapData.Scan0;
            Bitmap1.UnlockBits(&bitmapData); //<-- put breakpoint here

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            S Offline
            S Offline
            SandipG
            wrote on last edited by
            #5

            I played with it but it works with pens and brushes only. No change in effect of DrawImage.

            M 1 Reply Last reply
            0
            • S SandipG

              I played with it but it works with pens and brushes only. No change in effect of DrawImage.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              With DrawImage(), on the destination Graphics... SetCompositingMode(CompositingModeSourceCopy) will cause the drawn image to replace whatever was in the destination image. SetCompositingMode(CompositingModeSourceOver) will cause the drawn image to be blended with whatever was in the destination image (the resulting alpha values are not always set to 255). You should get the same results as with a pen and a brush. A bitmap filled with values with alpha set to 0 drawn on an image with alpha set to, say, 0x80, will leave the destination alpha 80. That matches your replace 0s with the first column alpha value, just like with a brush or pen. Regardless, I guess my point was that to get the results you want with the alpha channels, I don't believe the APIs are going to do it. Even though you were looking to avoid it, LockBits/UnlockBits makes it relatively trivial to loop through the images and combine them yourself. Mark -- modified at 13:09 Monday 25th June, 2007

              "Go that way, really fast. If something gets in your way, turn."

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups