public static IconHandle Create(ImageSource imageSource) { IconHandle result = IconHandle.Invalid; if (imageSource != null) { // Initialize bitmap rectangle and try to cast image source to bitmap frame Rect bitmapRect = new Rect(new Size(imageSource.Width, imageSource.Height)); BitmapFrame bitmapFrame = imageSource as BitmapFrame; if (bitmapFrame == null || !(bitmapFrame.Decoder is IconBitmapDecoder)) { // Create drawing visual DrawingVisual drawingVisual = new DrawingVisual(); // Open drawing context and draw the image to the visual using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { // Render the image and close drawing context drawingContext.DrawImage(imageSource, bitmapRect); } // Create render target bitmap and render the visual RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((Int32)bitmapRect.Width, (Int32)bitmapRect.Height, 96.0d, 96.0d, PixelFormats.Pbgra32); renderTargetBitmap.Render(drawingVisual); renderTargetBitmap.Freeze(); // Create bitmap frame from the render target bitmap bitmapFrame = BitmapFrame.Create(renderTargetBitmap); } // Copy pixels from bitmap frame to pixel array Int32 stride = (bitmapFrame.Format.BitsPerPixel * bitmapFrame.PixelWidth + 31) / 32 * 4; Byte[] pixelArray = new Byte[stride * bitmapFrame.PixelHeight]; bitmapFrame.CopyPixels(pixelArray, stride, 0); // Mask & icon bitmap handles IntPtr maskBitmapPtr = IntPtr.Zero; IntPtr iconBitmapPtr = IntPtr.Zero; // Initialize BITMAPINFO/BITMAPINFOHEADER structure NativeMethods.BITMAPINFO bmi = new NativeMethods.BITMAPINFO(); bmi.bmiHeader.biSize = (UInt32)Marshal.SizeOf(typeof(NativeMethods.BITMAPINFOHEADER)); bmi.bmiHeader.biWidth = (Int32)bitmapRect.Width; bmi.bmiHeader.biHeight -= ((Int32)bitmapRect.Height); bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = (UInt16)bitmapFrame.Format.BitsPerPixel; // DIB handle IntPtr dibPtr = IntPtr.Zero; // Create icon bitmap DIB section iconBitmapPtr = NativeMethods.CreateDIBSection(IntPtr.Zero, ref bmi, 0, out dibPtr, IntPtr.Zero, 0); // Validate icon bitmap handle if (iconBitmapPtr != IntPtr.Zero && dibPtr != IntPtr.Zero) { // Copy pixels to unmanaged memory Marshal.Copy(pixelArray, 0, dibPtr, pixelArray.Length); // Create mask bitmap maskBitmapPtr = NativeMethods.CreateBitmap((Int32)bitmapRect.Width, (Int32)bitmapRect.Height, 1, 1, IntPtr.Zero); if (maskBitmapPtr != IntPtr.Zero) { // Create and initialize ICONINFO structure NativeMethods.ICONINFO iconInfo = new NativeMethods.ICONINFO(); iconInfo.fIcon = true; iconInfo.hbmMask = maskBitmapPtr; iconInfo.hbmColor = iconBitmapPtr; // Create icon handle IntPtr iconPtr = NativeMethods.CreateIconIndirect(ref iconInfo); if (iconPtr != IntPtr.Zero) { result = new IconHandle(iconPtr); } } } // Clean up resources if (maskBitmapPtr != IntPtr.Zero) { NativeMethods.DeleteObject(maskBitmapPtr); } if (iconBitmapPtr != IntPtr.Zero) { NativeMethods.DeleteObject(iconBitmapPtr); } } // Return result return result; }
public static IconHandle Create(ImageSource imageSource) { IconHandle result = IconHandle.Invalid; if (imageSource != null) { // Initialize bitmap rectangle and try to cast image source to bitmap frame Rect bitmapRect = new Rect(new Size(imageSource.Width, imageSource.Height)); BitmapFrame bitmapFrame = imageSource as BitmapFrame; if (bitmapFrame == null || !(bitmapFrame.Decoder is IconBitmapDecoder)) { // Create drawing visual DrawingVisual drawingVisual = new DrawingVisual(); // Open drawing context and draw the image to the visual using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { // Render the image and close drawing context drawingContext.DrawImage(imageSource, bitmapRect); } // Create render target bitmap and render the visual RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((Int32)bitmapRect.Width, (Int32)bitmapRect.Height, 96.0d, 96.0d, PixelFormats.Pbgra32); renderTargetBitmap.Render(drawingVisual); renderTargetBitmap.Freeze(); // Create bitmap frame from the render target bitmap bitmapFrame = BitmapFrame.Create(renderTargetBitmap); } // Copy pixels from bitmap frame to pixel array Int32 stride = (bitmapFrame.Format.BitsPerPixel * bitmapFrame.PixelWidth + 31) / 32 * 4; Byte[] pixelArray = new Byte[stride * bitmapFrame.PixelHeight]; bitmapFrame.CopyPixels(pixelArray, stride, 0); // Mask & icon bitmap handles IntPtr maskBitmapPtr = IntPtr.Zero; IntPtr iconBitmapPtr = IntPtr.Zero; // Initialize BITMAPINFO/BITMAPINFOHEADER structure NativeMethods.BITMAPINFO bmi = new NativeMethods.BITMAPINFO(); bmi.bmiHeader.biSize = (UInt32)Marshal.SizeOf(typeof(NativeMethods.BITMAPINFOHEADER)); bmi.bmiHeader.biWidth = (Int32)bitmapRect.Width; bmi.bmiHeader.biHeight -= ((Int32)bitmapRect.Height); bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = (UInt16)bitmapFrame.Format.BitsPerPixel; // DIB handle IntPtr dibPtr = IntPtr.Zero; // Create icon bitmap DIB section iconBitmapPtr = NativeMethods.CreateDIBSection(IntPtr.Zero, ref bmi, 0, out dibPtr, IntPtr.Zero, 0); // Validate icon bitmap handle if (iconBitmapPtr != IntPtr.Zero && dibPtr != IntPtr.Zero) { // Copy pixels to unmanaged memory Marshal.Copy(pixelArray, 0, dibPtr, pixelArray.Length); // Create mask bitmap maskBitmapPtr = NativeMethods.CreateBitmap((Int32)bitmapRect.Width, (Int32)bitmapRect.Height, 1, 1, IntPtr.Zero); if (maskBitmapPtr != IntPtr.Zero) { // Create and initialize ICONINFO structure NativeMethods.ICONINFO iconInfo = new NativeMethods.ICONINFO(); iconInfo.fIcon = true; iconInfo.hbmMask = maskBitmapPtr; iconInfo.hbmColor = iconBitmapPtr; // Create icon handle IntPtr iconPtr = NativeMethods.CreateIconIndirect(ref iconInfo); if (iconPtr != IntPtr.Zero) { result = new IconHandle(iconPtr); } } } // Clean up resources if (maskBitmapPtr != IntPtr.Zero) { NativeMethods.DeleteObject(maskBitmapPtr); } if (iconBitmapPtr != IntPtr.Zero) { NativeMethods.DeleteObject(iconBitmapPtr); } } // Return result return(result); }