Toolbar image transparency

W

Wayne

I have a form with a tool bar and an image list. In the image list I've
added some 16x16 bmps, and have set the image list transparent color to the
background of these images. However the background still draws, is there
anyway to make the background of the images transparent?
 
Y

Ying-Shen Yu[MSFT]

Hi Wayne,

Here is a short code snippet I use for test, it works fine on my system.
You may try it to see if it could display your bmp files correctly.
<code>
private void InitializeToolbar()
{
ToolBar tb = new ToolBar ();
ToolBarButton tbb = new ToolBarButton("Test");
ImageList imgs = new ImageList();

//Imageslist
imgs.Images.Add( Image.FromFile (@"..\..\test.bmp"));//Your bmp file
imgs.TransparentColor = Color.Magenta; // the background color of your bmp
file

//ToolBarButton
tbb.ImageIndex = 0;

//Toolbar
tb.ImageList = imgs;
tb.Dock = DockStyle.Top;
tb.Buttons.Add ( tbb );

//Form
Controls.Add ( tb );
}
</code>

If this code snippet still does not displays the background color, you may
check if TransparentColor is identical to the background color of the
bitmap. If it still does not work, please send me that bitmap file, I'll
take a look.

Good Luck!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
S

Shaival Trivedi

First use 8 bit png images and then check it works or not .. If it still not
works then use this code
it adds trans.. imgae in image list

[DllImport("comctl32.dll")]
static extern bool ImageList_Add( IntPtr hImageList, IntPtr hBitmap,
IntPtr hMask );
[DllImport("kernel32.dll")]
static extern bool RtlMoveMemory( IntPtr dest, IntPtr source, int
dwcount);
[DllImport("gdi32.dll")]
static extern IntPtr CreateDIBSection(IntPtr hdc, [In,
MarshalAs(UnmanagedType.LPStruct)] BITMAPINFO pbmi, uint iUsage, out IntPtr
ppvBits, IntPtr hSection, uint dwOffset);

[StructLayout(LayoutKind.Explicit)]
public class BITMAPINFO
{
[FieldOffset(0)]
public Int32 biSize;
[FieldOffset(4)]
public Int32 biWidth;
[FieldOffset(8)]
public Int32 biHeight;
[FieldOffset(12)]
public Int16 biPlanes;
[FieldOffset(14)]
public Int16 biBitCount;
[FieldOffset(16)]
public Int32 biCompression;
[FieldOffset(20)]
public Int32 biSizeImage;
[FieldOffset(24)]
public Int32 biXPelsPerMeter;
[FieldOffset(28)]
public Int32 biYPelsPerMeter;
[FieldOffset(32)]
public Int32 biClrUsed;
[FieldOffset(36)]
public Int32 biClrImportant;
[FieldOffset(40)]
public Int32 colors;
};

void AddPNGToImageList( ImageList il, string szFileName )
{
Bitmap bm = new Bitmap( szFileName );
IntPtr hBitmap, ppvBits;
BITMAPINFO bmi = new BITMAPINFO();

bmi.biSize = 40;
bmi.biBitCount = 32;
bmi.biPlanes = 1;
bmi.biWidth = bm.Width;
bmi.biHeight = bm.Height;
hBitmap = CreateDIBSection( new IntPtr(0), bmi, 0, out ppvBits, new
IntPtr(0), 0 );
BitmapData bitmapData;
Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height );
bitmapData = bm.LockBits( rect, ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb );
IntPtr pixels = bitmapData.Scan0;
RtlMoveMemory( ppvBits, pixels, bm.Height*bitmapData.Stride );
bm.UnlockBits( bitmapData );
ImageList_Add( il.Handle, hBitmap, new IntPtr(0) );
}
//end code
 
W

Wayne

The code works, my Red bitmap shows up as blank when I set the background
color to red. Question now is, why doesn't it work this way when setting the
transparent color in the property editor at design time?

I also tried to modify the code to add a button to the existing toolbar on
my control and it still doesn't show the transparency correctly.

Any other ideas? I really don't want to have to ship bmp's with my
application for it to load at runtime. I'd like to be able to add them to
the image list and have the images displayed correctly.

Thanks
Wayne
 
W

Wayne

After playing with it a bit more, and the code giving me the idea I finally
got it working.

What I had to do is the following, and in this order assuming fresh
toolbar/image list:

1) Set the Transparent color on the image list
2) add the images
3) set the image index on each of the buttons

Apparently once the buttons are set they then have whatever value for
transparency is set on the image list at the time, changing the image list
doesn't cause the buttons to update. So for my existing app, I had to remove
all the images, set the transparency, then set all the buttons image index
to NONE, then set it back to what they were.

Thanks
Wayne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top