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. Semi-Transparent icons in ListView

Semi-Transparent icons in ListView

Scheduled Pinned Locked Moved C#
graphicsquestioncomlinuxhelp
9 Posts 2 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.
  • A Offline
    A Offline
    Ankit Rajpoot
    wrote on last edited by
    #1

    Hello everyone, I'm developing an application that is similar to Windows Explorer. I'm using a ListView control to display files and folders along with their icons. Now, icons for files can be retrieved using standard shell functions ExtranctIcon and ExtractIconEx. But my question is that how can I get that translucent effect as used by Windows Explorer for hidden and system files. I searched google and found the following code snippet:

    public static Image SetImageOpacity(Image imagePic, float imageOpacity)
    {
    Bitmap bmpPic = new Bitmap(imagePic.Width, imagePic.Height);
    Graphics gfxPic = Graphics.FromImage(bmpPic);
    ColorMatrix cmxPic = new ColorMatrix();
    cmxPic.Matrix33 = imageOpacity;

            ImageAttributes iaPic = new ImageAttributes();
            iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            gfxPic.DrawImage(imagePic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imagePic.Width, imagePic.Height, GraphicsUnit.Pixel, iaPic);
            gfxPic.Dispose();
    
            return bmpPic;
        }
    

    Now my code calls this method as follows:

    lViewVFSBrowser.LargeImageList.Images.Add(imageKey, SetImageOpacity(iconL.ToBitmap(), (float)0.7));

    but all I get in return is an icon with a bluish background as shown here: http://www.geocities.com/ankit_incredible_2006/problem.JPG[^]

    D 1 Reply Last reply
    0
    • A Ankit Rajpoot

      Hello everyone, I'm developing an application that is similar to Windows Explorer. I'm using a ListView control to display files and folders along with their icons. Now, icons for files can be retrieved using standard shell functions ExtranctIcon and ExtractIconEx. But my question is that how can I get that translucent effect as used by Windows Explorer for hidden and system files. I searched google and found the following code snippet:

      public static Image SetImageOpacity(Image imagePic, float imageOpacity)
      {
      Bitmap bmpPic = new Bitmap(imagePic.Width, imagePic.Height);
      Graphics gfxPic = Graphics.FromImage(bmpPic);
      ColorMatrix cmxPic = new ColorMatrix();
      cmxPic.Matrix33 = imageOpacity;

              ImageAttributes iaPic = new ImageAttributes();
              iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
              gfxPic.DrawImage(imagePic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imagePic.Width, imagePic.Height, GraphicsUnit.Pixel, iaPic);
              gfxPic.Dispose();
      
              return bmpPic;
          }
      

      Now my code calls this method as follows:

      lViewVFSBrowser.LargeImageList.Images.Add(imageKey, SetImageOpacity(iconL.ToBitmap(), (float)0.7));

      but all I get in return is an icon with a bluish background as shown here: http://www.geocities.com/ankit_incredible_2006/problem.JPG[^]

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      [Edit] It looks like the same source after rereading the code you posted! Could you let me have the link so I can make sure I give credit in the code. I think you need to up your opacity value**[/Edit]** [Edit2]Ok, found the original here[^][/Edit2] I've looked through all the icons in every file in the System32 folder and the hidden style icons aren't there, so I guess that Windows uses some overlay and/or transparency trick to do it. This should work - it's an altered version that I found on the web some time ago, can't remember the source. Opacity = 0 to 255, works fine with .ico files.

      public Image AdjustImage(Image image, float opacity)
      {
      Bitmap bitmap = new Bitmap(image.Width, image.Height);
      using (Graphics g = Graphics.FromImage(bitmap))
      {
      ColorMatrix matrix = new ColorMatrix();
      matrix.Matrix33 = opacity;
      ImageAttributes attributes = new ImageAttributes();
      attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
      g.DrawImage(
      image, new Rectangle(
      0, 0, bitmap.Width, bitmap.Height),
      0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
      }
      return bitmap;
      }

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

      A 2 Replies Last reply
      0
      • D DaveyM69

        [Edit] It looks like the same source after rereading the code you posted! Could you let me have the link so I can make sure I give credit in the code. I think you need to up your opacity value**[/Edit]** [Edit2]Ok, found the original here[^][/Edit2] I've looked through all the icons in every file in the System32 folder and the hidden style icons aren't there, so I guess that Windows uses some overlay and/or transparency trick to do it. This should work - it's an altered version that I found on the web some time ago, can't remember the source. Opacity = 0 to 255, works fine with .ico files.

        public Image AdjustImage(Image image, float opacity)
        {
        Bitmap bitmap = new Bitmap(image.Width, image.Height);
        using (Graphics g = Graphics.FromImage(bitmap))
        {
        ColorMatrix matrix = new ColorMatrix();
        matrix.Matrix33 = opacity;
        ImageAttributes attributes = new ImageAttributes();
        attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        g.DrawImage(
        image, new Rectangle(
        0, 0, bitmap.Width, bitmap.Height),
        0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
        }
        return bitmap;
        }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        A Offline
        A Offline
        Ankit Rajpoot
        wrote on last edited by
        #3

        Thanks Dave for you time, but this method is exactly the same as the one I posted. Still I tried it, and used higher values for opacity (0 ~ 255) but the result is still the same.

        D 2 Replies Last reply
        0
        • A Ankit Rajpoot

          Thanks Dave for you time, but this method is exactly the same as the one I posted. Still I tried it, and used higher values for opacity (0 ~ 255) but the result is still the same.

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Yeah, I realized that after I posted it - hence the edits! I think it's something to do with the way the ListView renders it as it works fine elsewhere. I'll keep playing, there has to be a solution.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          1 Reply Last reply
          0
          • D DaveyM69

            [Edit] It looks like the same source after rereading the code you posted! Could you let me have the link so I can make sure I give credit in the code. I think you need to up your opacity value**[/Edit]** [Edit2]Ok, found the original here[^][/Edit2] I've looked through all the icons in every file in the System32 folder and the hidden style icons aren't there, so I guess that Windows uses some overlay and/or transparency trick to do it. This should work - it's an altered version that I found on the web some time ago, can't remember the source. Opacity = 0 to 255, works fine with .ico files.

            public Image AdjustImage(Image image, float opacity)
            {
            Bitmap bitmap = new Bitmap(image.Width, image.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
            ColorMatrix matrix = new ColorMatrix();
            matrix.Matrix33 = opacity;
            ImageAttributes attributes = new ImageAttributes();
            attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            g.DrawImage(
            image, new Rectangle(
            0, 0, bitmap.Width, bitmap.Height),
            0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
            }
            return bitmap;
            }

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            A Offline
            A Offline
            Ankit Rajpoot
            wrote on last edited by
            #5

            Hi Dave, I got it working correctly after a few hours of :( :sigh: :doh: :(( :-\ :-\ :-\ :wtf: :wtf: :wtf: . The information from Joe Pardue's articles on transparency and alpha blending got me on the right track. Really they are excellent articles. I'd recommend reading them to all those who want to get a grip on graphics processing concepts. Transparency Tutorial with C# - Part 1[^] Transparency Tutorial with C# - Part 2[^] Transparency Tutorial with C# - Part 3[^] All that was needed was this little modification:

            public static Image SetImageOpacity(Image image, float opacity)
            {
            Bitmap bitmap = new Bitmap(image.Width, image.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
            // The followine statement was absent originally.
            g.FillRegion(new SolidBrush(SystemColors.Window), new Region(new Rectangle(0, 0, image.Width, image.Height)));
            ColorMatrix matrix = new ColorMatrix();
            matrix.Matrix33 = opacity;
            ImageAttributes attributes = new ImageAttributes();
            attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
            }
            return bitmap;
            }

            Originally the method did the following: 1. Create a new Bitmap object. 2. Create a new ColorMatrix object and set the alpha element (3,3) to the desired value. 3. Construct a new ImageAttributes object, associate the ColorMatrix with it, and then use this whole information to draw the input Image over the new Bitmap object. The problem, as far as I understand now (after reading Joe's articles), was that the method was trying to draw the source image over an empty bitmap (black colored bitmap). Thus, after applying the transformation, the image goes dark instead of fading into the background color. Now the line

            D 2 Replies Last reply
            0
            • A Ankit Rajpoot

              Thanks Dave for you time, but this method is exactly the same as the one I posted. Still I tried it, and used higher values for opacity (0 ~ 255) but the result is still the same.

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              Actually, it appears it's the ImageList that's causing the problem.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

              1 Reply Last reply
              0
              • A Ankit Rajpoot

                Hi Dave, I got it working correctly after a few hours of :( :sigh: :doh: :(( :-\ :-\ :-\ :wtf: :wtf: :wtf: . The information from Joe Pardue's articles on transparency and alpha blending got me on the right track. Really they are excellent articles. I'd recommend reading them to all those who want to get a grip on graphics processing concepts. Transparency Tutorial with C# - Part 1[^] Transparency Tutorial with C# - Part 2[^] Transparency Tutorial with C# - Part 3[^] All that was needed was this little modification:

                public static Image SetImageOpacity(Image image, float opacity)
                {
                Bitmap bitmap = new Bitmap(image.Width, image.Height);
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                // The followine statement was absent originally.
                g.FillRegion(new SolidBrush(SystemColors.Window), new Region(new Rectangle(0, 0, image.Width, image.Height)));
                ColorMatrix matrix = new ColorMatrix();
                matrix.Matrix33 = opacity;
                ImageAttributes attributes = new ImageAttributes();
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
                }
                return bitmap;
                }

                Originally the method did the following: 1. Create a new Bitmap object. 2. Create a new ColorMatrix object and set the alpha element (3,3) to the desired value. 3. Construct a new ImageAttributes object, associate the ColorMatrix with it, and then use this whole information to draw the input Image over the new Bitmap object. The problem, as far as I understand now (after reading Joe's articles), was that the method was trying to draw the source image over an empty bitmap (black colored bitmap). Thus, after applying the transformation, the image goes dark instead of fading into the background color. Now the line

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                Cool! It's wierd, as setting a form's BackgroundImage worked fine before if set from the function but not if set from the image list after filling it from the function :confused: Anyway, nice to know it's sorted and thanks for posting the solution :-D

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                1 Reply Last reply
                0
                • A Ankit Rajpoot

                  Hi Dave, I got it working correctly after a few hours of :( :sigh: :doh: :(( :-\ :-\ :-\ :wtf: :wtf: :wtf: . The information from Joe Pardue's articles on transparency and alpha blending got me on the right track. Really they are excellent articles. I'd recommend reading them to all those who want to get a grip on graphics processing concepts. Transparency Tutorial with C# - Part 1[^] Transparency Tutorial with C# - Part 2[^] Transparency Tutorial with C# - Part 3[^] All that was needed was this little modification:

                  public static Image SetImageOpacity(Image image, float opacity)
                  {
                  Bitmap bitmap = new Bitmap(image.Width, image.Height);
                  using (Graphics g = Graphics.FromImage(bitmap))
                  {
                  // The followine statement was absent originally.
                  g.FillRegion(new SolidBrush(SystemColors.Window), new Region(new Rectangle(0, 0, image.Width, image.Height)));
                  ColorMatrix matrix = new ColorMatrix();
                  matrix.Matrix33 = opacity;
                  ImageAttributes attributes = new ImageAttributes();
                  attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                  g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
                  }
                  return bitmap;
                  }

                  Originally the method did the following: 1. Create a new Bitmap object. 2. Create a new ColorMatrix object and set the alpha element (3,3) to the desired value. 3. Construct a new ImageAttributes object, associate the ColorMatrix with it, and then use this whole information to draw the input Image over the new Bitmap object. The problem, as far as I understand now (after reading Joe's articles), was that the method was trying to draw the source image over an empty bitmap (black colored bitmap). Thus, after applying the transformation, the image goes dark instead of fading into the background color. Now the line

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  That works only if the BackColor of the ListView is SystemColors.Window and you don't use the image on any other control with a different BackColor. Adding bitmap.MakeTransparent(SystemColors.Window); before returning fixes that.

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  A 1 Reply Last reply
                  0
                  • D DaveyM69

                    That works only if the BackColor of the ListView is SystemColors.Window and you don't use the image on any other control with a different BackColor. Adding bitmap.MakeTransparent(SystemColors.Window); before returning fixes that.

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                    A Offline
                    A Offline
                    Ankit Rajpoot
                    wrote on last edited by
                    #9

                    Yeah, I noticed that. Thanks for pointing out. :thumbsup::thumbsup:

                    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