Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/System.Windows.Forms/Resources/SR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3447,6 +3447,9 @@ Stack trace where the illegal operation occurred was:
<data name="ImageListTransparentColorDescr" xml:space="preserve">
<value>The color that is treated as transparent.</value>
</data>
<data name="ImageListUseMaskForTransparencyDescr" xml:space="preserve">
<value>Gets or sets whether to use a mask for transparency.</value>
</data>
<data name="IndexOutOfRange" xml:space="preserve">
<value>Index {0} is out of range.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,34 @@ public Image this[int index]

try
{
HBITMAP hMask = (HBITMAP)ControlPaint.CreateHBitmapTransparencyMask(bitmap);
HBITMAP hBitmap = (HBITMAP)ControlPaint.CreateHBitmapColorMask(bitmap, hMask);
bool ok;
try
if (!_owner.UseMaskForTransparency)
{
ok = PInvoke.ImageList.Replace(_owner, index, hBitmap, hMask);
HBITMAP hBitmap = (HBITMAP)bitmap.GetHbitmap();
try
{
using var ilHandle = new DeleteObjectSafeHandle(_owner.Handle, ownsHandle: false);
ok = PInvoke.ImageList_Replace(ilHandle, index, hBitmap, default);
}
finally
{
PInvokeCore.DeleteObject(hBitmap);
}
}
finally
else
{
PInvokeCore.DeleteObject((HGDIOBJ)hBitmap);
PInvokeCore.DeleteObject((HGDIOBJ)hMask);
HBITMAP hMask = (HBITMAP)ControlPaint.CreateHBitmapTransparencyMask(bitmap);
HBITMAP hBitmap = (HBITMAP)ControlPaint.CreateHBitmapColorMask(bitmap, hMask);
bool ok;
try
{
ok = PInvoke.ImageList.Replace(_owner, index, hBitmap, hMask);
}
finally
{
PInvokeCore.DeleteObject((HGDIOBJ)hBitmap);
PInvokeCore.DeleteObject((HGDIOBJ)hMask);
}
}

if (!ok)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ public ImageListStreamer? ImageStream

private bool UseTransparentColor => TransparentColor.A > 0;

/// <summary>
/// Gets or sets whether to use a mask for transparency.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[SRDescription(nameof(SR.ImageListUseMaskForTransparencyDescr))]
public bool UseMaskForTransparency { get; set; } = true;

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[SRDescription(nameof(SR.ImageListOnRecreateHandleDescr))]
Expand Down Expand Up @@ -370,21 +377,37 @@ private int AddToHandle(Bitmap bitmap)
{
Debug.Assert(HandleCreated, "Calling AddToHandle when there is no handle");

// Calls GDI to create Bitmap.
HBITMAP hMask = (HBITMAP)ControlPaint.CreateHBitmapTransparencyMask(bitmap);

// Calls GDI+ to create Bitmap
HBITMAP hBitmap = (HBITMAP)ControlPaint.CreateHBitmapColorMask(bitmap, (IntPtr)hMask);

int index;
try
if (!UseMaskForTransparency)
{
index = PInvoke.ImageList.Add(this, hBitmap, hMask);
HBITMAP hBitmap = (HBITMAP)bitmap.GetHbitmap();
try
{
using var ilHandle = new DeleteObjectSafeHandle(Handle, ownsHandle: false);
index = PInvoke.ImageList_Add(ilHandle, hBitmap, default);
}
finally
{
PInvokeCore.DeleteObject(hBitmap);
}
}
finally
else
{
PInvokeCore.DeleteObject(hBitmap);
PInvokeCore.DeleteObject(hMask);
// Calls GDI to create Bitmap.
HBITMAP hMask = (HBITMAP)ControlPaint.CreateHBitmapTransparencyMask(bitmap);

// Calls GDI+ to create Bitmap
HBITMAP hBitmap = (HBITMAP)ControlPaint.CreateHBitmapColorMask(bitmap, (IntPtr)hMask);

try
{
index = PInvoke.ImageList.Add(this, hBitmap, hMask);
}
finally
{
PInvokeCore.DeleteObject(hBitmap);
PInvokeCore.DeleteObject(hMask);
}
}

return index == -1 ? throw new InvalidOperationException(SR.ImageListAddFailed) : index;
Expand Down
Loading