Y
Yuval Naveh
Hello,
I have encountered a problem while trying to display images in the
ListView control.
I am using Visual Studio 2003/C# on windows 2000 professional SP4.
My images are 32 bit PNG with alpha channel that has 256 levels.
After adding them to an ImageList component, and attaching the image list
to a list view, the images show thier alpha channel pixels as opaque pixels.
The problem seems to be with ImageList.
This subject has come up already in this newsgroup, and I saw no solution
for the problem.
Someone posted an untested code that is supposed to fix the problem, it
compiles and runs but does not work.
Can anyone suggest a solution (a working and tested code)?
Thanks
Yuval Naveh
Actimize
Israel
The code that was supposed to fix the problem:
[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) );
}
I have encountered a problem while trying to display images in the
ListView control.
I am using Visual Studio 2003/C# on windows 2000 professional SP4.
My images are 32 bit PNG with alpha channel that has 256 levels.
After adding them to an ImageList component, and attaching the image list
to a list view, the images show thier alpha channel pixels as opaque pixels.
The problem seems to be with ImageList.
This subject has come up already in this newsgroup, and I saw no solution
for the problem.
Someone posted an untested code that is supposed to fix the problem, it
compiles and runs but does not work.
Can anyone suggest a solution (a working and tested code)?
Thanks
Yuval Naveh
Actimize
Israel
The code that was supposed to fix the problem:
[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) );
}