Semi-Transparent icons in ListView
-
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[^]
-
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[^]
[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) -
[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)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.
-
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.
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) -
[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)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
-
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.
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) -
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
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) -
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
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) -
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)Yeah, I noticed that. Thanks for pointing out. :thumbsup::thumbsup: