View Full Version : How to set the Alpha of a DotNet "System.Drawing.Image"?
Jausn 07-17-2009, 07:27 AM 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?
|
|
LoneRobot
07-20-2009, 08:28 AM
Hi,
your have two workable options with dotnet and transparency.
1. 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.
2. (My prefered choice) use 32bit PNG's. Windows handles these natively and automatically uses the alpha channel correctly. Much less hassle!
Jausn
07-20-2009, 09:59 AM
Hi,
2. (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:
http://b19.photo.store.qq.com/http_imgload.cgi?/rurl4_b=8975167f7c17b76e44494f51a38222d4b5dc612751d794de38a0a92f024ef935fa3baa9e767e114c275040347cf54e053dda19e42d4740dc04b61bd242016f1cac2568119f3e64b4dba204996401a20860b4d717
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
)
LoneRobot
07-20-2009, 10:07 AM
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:
http://b19.photo.store.qq.com/http_imgload.cgi?/rurl4_b=8975167f7c17b76e44494f51a38222d4b5dc612751d794de38a0a92f024ef935fa3baa9e767e114c275040347cf54e053dda19e42d4740dc04b61bd242016f1cac2568119f3e64b4dba204996401a20860b4d717
not really following, how is it supposed to look?
denisT
07-20-2009, 07:13 PM
ilTv = dotNetObject "System.Windows.Forms.ImageList"
ilTv.imageSize = dotNetObject "System.Drawing.Size" 16 15
ilTv.colorDepth = ilTv.colorDepth.Depth32Bit
/*ilTv.transparentColor = <any that you want> */
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.
LoneRobot
07-20-2009, 09:07 PM
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.
Jausn
07-21-2009, 03:38 AM
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.
Jausn
07-21-2009, 04:08 AM
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?
LoneRobot
07-21-2009, 09:28 AM
indeed, preferably an opposing bright one so that it wont mask any pixels you want.
Jausn
07-23-2009, 02:45 AM
indeed, preferably an opposing bright one so that it wont mask any pixels you want.
Thank you. But I don't get you very much. Could you please show an example?
LoneRobot
07-23-2009, 09:05 AM
something like this -
with this example you would set the transparent color of the image list to white, masking out the background only
CGTalk Moderation
07-23-2009, 09:05 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.