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. How can I make an Image tranparent

How can I make an Image tranparent

Scheduled Pinned Locked Moved C#
helpquestion
10 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.
  • N Offline
    N Offline
    Naveed727
    wrote on last edited by
    #1

    Please help me out. I have an image and want to make it transparent. I didn't know how I will do this any idea will be helpful for me. Thanks in advance.

    L 1 Reply Last reply
    0
    • N Naveed727

      Please help me out. I have an image and want to make it transparent. I didn't know how I will do this any idea will be helpful for me. Thanks in advance.

      L Offline
      L Offline
      Lev Danielyan
      wrote on last edited by
      #2

      First create a buffer image of the same size and resolution as the source image. Then draw the source image on the buffer, specifying the transparent color using the ImageAttributes. Something like this:

      Image image = Image.FromFile(@"c:\logo.png");
      Bitmap buffer = new Bitmap(image.Width, image.Height);
      buffer.SetResolution(image.HorizontalResolution,image.VerticalResolution);

      Rectangle destRect = new Rectangle(0, 0, image.Width, image.Height);

      ImageAttributes attributes = new ImageAttributes();

      // Set the transparent color
      attributes.SetColorKey(Color.Red, Color.Red);

      using (Graphics gr = Graphics.FromImage(buffer)) {
      gr.DrawImage(image, destRect, 0, 0, image.Width,
      image.Height, GraphicsUnit.Pixel, attributes);
      }

      buffer.Save(@"c:\logo2.png");

      Regards, Lev

      N 1 Reply Last reply
      0
      • L Lev Danielyan

        First create a buffer image of the same size and resolution as the source image. Then draw the source image on the buffer, specifying the transparent color using the ImageAttributes. Something like this:

        Image image = Image.FromFile(@"c:\logo.png");
        Bitmap buffer = new Bitmap(image.Width, image.Height);
        buffer.SetResolution(image.HorizontalResolution,image.VerticalResolution);

        Rectangle destRect = new Rectangle(0, 0, image.Width, image.Height);

        ImageAttributes attributes = new ImageAttributes();

        // Set the transparent color
        attributes.SetColorKey(Color.Red, Color.Red);

        using (Graphics gr = Graphics.FromImage(buffer)) {
        gr.DrawImage(image, destRect, 0, 0, image.Width,
        image.Height, GraphicsUnit.Pixel, attributes);
        }

        buffer.Save(@"c:\logo2.png");

        Regards, Lev

        N Offline
        N Offline
        Naveed727
        wrote on last edited by
        #3

        Thanks for your reply. This example very helpful but I want to make transparent whole image not just one or two colors. I hope you can give me a good idea. Regards

        L 1 Reply Last reply
        0
        • N Naveed727

          Thanks for your reply. This example very helpful but I want to make transparent whole image not just one or two colors. I hope you can give me a good idea. Regards

          L Offline
          L Offline
          Lev Danielyan
          wrote on last edited by
          #4

          Naveed khan nido wrote:

          I want to make transparent whole image

          What do you mean? if you want the whole image to be transparent, just create a new image object and save it right away. That's not one or two colors, the SetColorKey accepts two color objects to specify the range of colors to make transparent.

          Regards, Lev

          N 1 Reply Last reply
          0
          • L Lev Danielyan

            Naveed khan nido wrote:

            I want to make transparent whole image

            What do you mean? if you want the whole image to be transparent, just create a new image object and save it right away. That's not one or two colors, the SetColorKey accepts two color objects to specify the range of colors to make transparent.

            Regards, Lev

            N Offline
            N Offline
            Naveed727
            wrote on last edited by
            #5

            Yes I want to transparent whole Image. What do you mean to create an object of image and save it. But I am going to transparent the whole image. the example you given is just removing the desire color. I create an black image and give black color parameter in you example it remove the black color but not showing the other side control. If you can give me hit to properly transplant a single color then I will create a loop for all color. Regards

            L 1 Reply Last reply
            0
            • N Naveed727

              Yes I want to transparent whole Image. What do you mean to create an object of image and save it. But I am going to transparent the whole image. the example you given is just removing the desire color. I create an black image and give black color parameter in you example it remove the black color but not showing the other side control. If you can give me hit to properly transplant a single color then I will create a loop for all color. Regards

              L Offline
              L Offline
              Lev Danielyan
              wrote on last edited by
              #6

              Well, let me say again. If you want to make the whole image transparent, this is equal to creating an empty image of the same size and resolution and saving it, i.e. dropping the content of the image. You could have modified the code I've sent pretty easy, but anyway, here it is:

              Image img = Image.FromFile(@"c:\logo.png");
              Bitmap buffer = new Bitmap(img.Width, img.Height);
              buffer.SetResolution(img.HorizontalResolution, img.VerticalResolution);
              buffer.Save(@"c:\logo2.png");

              This will give you the same source image, BUT without the content (I actually wonder what is this for ;))

              Regards, Lev

              N L 2 Replies Last reply
              0
              • L Lev Danielyan

                Well, let me say again. If you want to make the whole image transparent, this is equal to creating an empty image of the same size and resolution and saving it, i.e. dropping the content of the image. You could have modified the code I've sent pretty easy, but anyway, here it is:

                Image img = Image.FromFile(@"c:\logo.png");
                Bitmap buffer = new Bitmap(img.Width, img.Height);
                buffer.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                buffer.Save(@"c:\logo2.png");

                This will give you the same source image, BUT without the content (I actually wonder what is this for ;))

                Regards, Lev

                N Offline
                N Offline
                Naveed727
                wrote on last edited by
                #7

                Thank you very muck.

                L 1 Reply Last reply
                0
                • N Naveed727

                  Thank you very muck.

                  L Offline
                  L Offline
                  Lev Danielyan
                  wrote on last edited by
                  #8

                  You're welcome

                  Regards, Lev

                  1 Reply Last reply
                  0
                  • L Lev Danielyan

                    Well, let me say again. If you want to make the whole image transparent, this is equal to creating an empty image of the same size and resolution and saving it, i.e. dropping the content of the image. You could have modified the code I've sent pretty easy, but anyway, here it is:

                    Image img = Image.FromFile(@"c:\logo.png");
                    Bitmap buffer = new Bitmap(img.Width, img.Height);
                    buffer.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                    buffer.Save(@"c:\logo2.png");

                    This will give you the same source image, BUT without the content (I actually wonder what is this for ;))

                    Regards, Lev

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Lev Danielyan wrote:

                    I actually wonder what is this for

                    you can use a glass plate like that to fix a broken window. :laugh:

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Love, happiness and fewer bugs for 2009!


                    L 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Lev Danielyan wrote:

                      I actually wonder what is this for

                      you can use a glass plate like that to fix a broken window. :laugh:

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Love, happiness and fewer bugs for 2009!


                      L Offline
                      L Offline
                      Lev Danielyan
                      wrote on last edited by
                      #10

                      Right :laugh: :laugh:

                      Regards, Lev

                      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