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. C#
  4. Change background color of an image

Change background color of an image

Scheduled Pinned Locked Moved C#
graphicstutorialquestion
7 Posts 3 Posters 0 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.
  • X Offline
    X Offline
    Xmen Real
    wrote on last edited by
    #1

    i want to change the background color of an image in that way that only visible part get new background color, is there any in built function for this ? well, i tried with this but i dont know what to do next

    Bitmap bmp = new Bitmap("C:\abc.png");
    for (int x = 0; x < bmp.Width; x++)
    {
    for (int y = 0; y < bmp.Height; y++)
    {
    Color tmp_oldColor = bmp.GetPixel(x, y);
    if (tmp_oldColor != Color.Transparent)
    {
    Color tmp_newColor = ??; // how to get new color ? Multiply, Add ?
    tmp_newColor.A = tmp_oldColor.A;
    bmp.SetPixel(x, y, tmp_newColor);
    }
    }
    }

    TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

    G P 2 Replies Last reply
    0
    • X Xmen Real

      i want to change the background color of an image in that way that only visible part get new background color, is there any in built function for this ? well, i tried with this but i dont know what to do next

      Bitmap bmp = new Bitmap("C:\abc.png");
      for (int x = 0; x < bmp.Width; x++)
      {
      for (int y = 0; y < bmp.Height; y++)
      {
      Color tmp_oldColor = bmp.GetPixel(x, y);
      if (tmp_oldColor != Color.Transparent)
      {
      Color tmp_newColor = ??; // how to get new color ? Multiply, Add ?
      tmp_newColor.A = tmp_oldColor.A;
      bmp.SetPixel(x, y, tmp_newColor);
      }
      }
      }

      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      As you are not using the transparency to determine background color, what do you want the background color to be?

      Despite everything, the person most likely to be fooling you next is yourself.

      X 1 Reply Last reply
      0
      • G Guffa

        As you are not using the transparency to determine background color, what do you want the background color to be?

        Despite everything, the person most likely to be fooling you next is yourself.

        X Offline
        X Offline
        Xmen Real
        wrote on last edited by
        #3

        hmmm....my mistake, i wrote "change". The actual thing is that the image is transparent, like vista glass and but have some shapes on it those are tranparent too and i want to set its backcolor, like green, blue or any. That my question.

        TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

        G 1 Reply Last reply
        0
        • X Xmen Real

          i want to change the background color of an image in that way that only visible part get new background color, is there any in built function for this ? well, i tried with this but i dont know what to do next

          Bitmap bmp = new Bitmap("C:\abc.png");
          for (int x = 0; x < bmp.Width; x++)
          {
          for (int y = 0; y < bmp.Height; y++)
          {
          Color tmp_oldColor = bmp.GetPixel(x, y);
          if (tmp_oldColor != Color.Transparent)
          {
          Color tmp_newColor = ??; // how to get new color ? Multiply, Add ?
          tmp_newColor.A = tmp_oldColor.A;
          bmp.SetPixel(x, y, tmp_newColor);
          }
          }
          }

          TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

          P Offline
          P Offline
          PandemoniumPasha
          wrote on last edited by
          #4

          Xmen wrote:

          Color tmp_newColor = ??; // how to get new color ? Multiply, Add ?

          Color tmp_newColor = Color.Green; or Color.FromArgb(....) or Color.FromName(...) or Color.FromKnownName(...) choose whatever... HTH [Modified] one more thing, you are using GetPixel and SetPixel which are extremely slow. try using LockBits() and UnlockBits() for speed. [/Modified]

          regards :)

          X 1 Reply Last reply
          0
          • X Xmen Real

            hmmm....my mistake, i wrote "change". The actual thing is that the image is transparent, like vista glass and but have some shapes on it those are tranparent too and i want to set its backcolor, like green, blue or any. That my question.

            TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Ok, create a new bitmap with the same size, create a graphics object for it using Graphics.FromImage, fill the image with the background image using the Clear method and draw your image on it using the DrawImage method.

            Despite everything, the person most likely to be fooling you next is yourself.

            X 1 Reply Last reply
            0
            • P PandemoniumPasha

              Xmen wrote:

              Color tmp_newColor = ??; // how to get new color ? Multiply, Add ?

              Color tmp_newColor = Color.Green; or Color.FromArgb(....) or Color.FromName(...) or Color.FromKnownName(...) choose whatever... HTH [Modified] one more thing, you are using GetPixel and SetPixel which are extremely slow. try using LockBits() and UnlockBits() for speed. [/Modified]

              regards :)

              X Offline
              X Offline
              Xmen Real
              wrote on last edited by
              #6

              PandemoniumPasha wrote:

              Color tmp_newColor = Color.Green;or Color.FromArgb(....)or Color.FromName(...)or Color.FromKnownName(...)choose whatever...

              this is not that simple, i have to calculate the RGB values for new color.

              PandemoniumPasha wrote:

              one more thing, you are using GetPixel and SetPixel which are extremely slow. try using LockBits() and UnlockBits() for speed.

              thanks for the info ;)

              TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

              1 Reply Last reply
              0
              • G Guffa

                Ok, create a new bitmap with the same size, create a graphics object for it using Graphics.FromImage, fill the image with the background image using the Clear method and draw your image on it using the DrawImage method.

                Despite everything, the person most likely to be fooling you next is yourself.

                X Offline
                X Offline
                Xmen Real
                wrote on last edited by
                #7

                No, you misunderstood, here is some example :) Transparent Image Added Red Back Color now see the last image, i added color red but i just want color back to the image, not in entire image i can do something nearly, Fill a rectangle with Red color, draw image then use MakeTransparent(Color.Red) but it wont do exactly what i want, it will not set alpha. My image wont be transparent anymore. the otherway i can use is : (just wrote directly, not tested)

                            Bitmap bmp = new Bitmap(ofd.FileName);
                            Bitmap bmp\_backColor = new Bitmap(bmp.Width, bmp.Height);
                            for (int x = 0; x < bmp.Width; x++)
                            {
                                for (int y = 0; y < bmp.Height; y++)
                                {
                                    Color tmp\_oldColor = bmp.GetPixel(x, y);
                                    if (tmp\_oldColor != Color.Transparent)
                                    {
                                        Color tmp\_newColor = Color.FromArgb(tmp\_oldColor.A, 255, 0, 0);
                                        bmp\_backColor.SetPixel(x, y, tmp\_newColor);
                                    }                        
                                }
                            }
                            Graphics m\_gr = Graphics.FromImage(bmp\_backColor);
                            m\_gr.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
                            m\_gr.Dispose();
                
                            pictureBox1.BackgroundImage = bmp\_backColor;
                

                Is this way right ? Modified : I updated the code with working one, The Working Output My question, is that the only way or is that way good ?

                TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

                modified on Tuesday, January 27, 2009 4:21 AM

                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