[Closed] How to set the Alpha of a DotNet "System.Drawing.Image"?
Hello everyone!
I got a question.
I created a dotNetClass “System.Drawing.Image” but I don’t know how to set it transparent. Can I make it transparent using another image as its Alpha channal to mask the unwanted part?
imgPath = "C:\Program Files\Autodesk\3ds Max 9\ui\Icons\LayerToolbar_16i.bmp"
imgAlPath = "C:\Program Files\Autodesk\3ds Max 9\ui\Icons\LayerToolbar_16a.bmp"
img = dotNetClass "System.Drawing.Image"
img.fromFile imgPath
-- how to mask the unwanted part with the imgAlPath?
Hi,
your have two workable options with dotnet and transparency.
-
Use an imagelist and bind it to the control. You are able to set a color to transparent in order to mask out areas of the image. Often, bmp icons have a crazy backcolor like magenta so that an image list can use it and not mask any icon color pixels out.
-
(My prefered choice) use 32bit PNG’s. Windows handles these natively and automatically uses the alpha channel correctly. Much less hassle!
I tried using 32bit png images which work pretty well. But the only prolbem is that not all png images can be masked correctly as I showed in the picture below:
You can see there’s some blue color appearing.
And the pngs are below:
So why ?
Code:
ilTv = dotNetObject "System.Windows.Forms.ImageList"
ilTv.imageSize = dotNetObject "System.Drawing.Size" 16 15
ilTv.colorDepth = ilTv.colorDepth.Depth32Bit
fn initTreeView tv =
(
tv.Indent = 10
tv.CheckBoxes = true
tv.LabelEdit = true
dNColor = dotNetClass "System.Drawing.Color"
tv.backColor = dNColor.fromArgb 50 50 50
tv.foreColor = dNColor.fromArgb 180 180 180
img = dotNetClass "System.Drawing.Image"
ilTv.images.add (img.fromFile "icon_Layer.png")
img = dotNetClass "System.Drawing.Image"
ilTv.images.add (img.fromFile "icon_Sphere.png")
img = dotNetClass "System.Drawing.Image"
ilTv.images.add (img.fromFile "icon_Light.png")
img = dotNetClass "System.Drawing.Image"
ilTv.images.add (img.fromFile "icon_Camera.png")
img = dotNetClass "System.Drawing.Image"
ilTv.images.add (img.fromFile "icon_Helper.png")
tv.imageList = ilTv
)
ilTv = dotNetObject "System.Windows.Forms.ImageList"
ilTv.imageSize = dotNetObject "System.Drawing.Size" 16 15
ilTv.colorDepth = ilTv.colorDepth.Depth32Bit
/*ilTv.transparentColor = [i]<any that you want> */[/i]
fn initTreeView tv =
(
tv.Indent = 10
tv.CheckBoxes = true
tv.LabelEdit = true
dNColor = dotNetClass "System.Drawing.Color"
tv.backColor = dNColor.fromArgb 50 50 50
tv.foreColor = dNColor.fromArgb 180 180 180
img = dotNetClass "System.Drawing.Image" /*call it only once.*/
ilTv.images.add (img.fromFile "icon_Layer.png")
ilTv.images.add (img.fromFile "icon_Sphere.png")
ilTv.images.add (img.fromFile "icon_Light.png")
ilTv.images.add (img.fromFile "icon_Camera.png")
ilTv.images.add (img.fromFile "icon_Helper.png")
tv.imageList = ilTv
)
I don’t understand why do you need to change image alphas. ImageList has a property “transparentColor”. Use any color in your icons to make them masked.
just to add to the code example, (thanks for posting this, denis) you dont need to specify a 32bit image list if you are using a mask color set as a transparent color of an image list, only if you wish to use the transparent pngs, but you cant specify a separate image as a mask in the way that max does icons on buttons. This is in theory possible with some GDI+ drawing, although i havent tried in light of the options already there.
Sorry,I didn’t konw this image could not be displayed correctly. So this is my interface picture.
And it can’t be all correct whether I use transparent color or png to set the icon transparent as you can see in the image.
I don’t understand why do you need to change image alphas. ImageList has a property “transparentColor”. Use any color in your icons to make them masked.
Thanks!
But I wanna use the icons in the max system dir like:
“C:\Program Files\Autodesk\3ds Max 9\ui\Icons\LayerToolbar_16i.bmp”
But you know the colors of parts that should be masked off are not all the same. Some might be 197 197 197, some might be 195 195 195.
And so if I set this:
ilTv.TransparentColor = (dotNetClass “System.Drawing.Color”).fromArgb 197 197 197
Then only some parts are masked off as the image below.
So do I have to create a new icon image and set the color of parts that should be masked off with a uniform color?
indeed, preferably an opposing bright one so that it wont mask any pixels you want.
something like this –
with this example you would set the transparent color of the image list to white, masking out the background only