public static Media.ImageSource BuildLargeImage(string tag, Media.Color color = default) { using (var g = Drawing.Graphics.FromHwnd(NX.MainWindowHandle)) { int pixelX = (int)Math.Round((g.DpiX / 96.0) * 32); int pixelY = (int)Math.Round((g.DpiY / 96.0) * 32); return(BuildImage(tag, 64, 64, color.ToDrawingColor()).ToBitmapImage(pixelX, pixelY)); } }
/// <summary> /// Gets a System.Drawing.Bitmap from a BitmapSource. /// </summary> /// <param name="source">The source image from which to create our Bitmap.</param> /// <param name="transparencyKey">The transparency key. This is used by the DragDropHelper /// in rendering transparent pixels.</param> /// <returns>An instance of Bitmap which is a copy of the BitmapSource's image.</returns> private static Bitmap GetBitmapFromBitmapSource(BitmapSource source, Color transparencyKey) { // Copy at full size Int32Rect sourceRect = new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight); // Convert to our destination pixel format DrawingPixelFormat pxFormat = ConvertPixelFormat(source.Format); // Create the Bitmap, full size, full rez Bitmap bmp = new Bitmap(sourceRect.Width, sourceRect.Height, pxFormat); // If the format is an indexed format, copy the color palette if ((pxFormat & DrawingPixelFormat.Indexed) == DrawingPixelFormat.Indexed) { ConvertColorPalette(bmp.Palette, source.Palette); } // Get the transparency key as a System.Drawing.Color DrawingColor transKey = transparencyKey.ToDrawingColor(); // Lock our Bitmap bits, we need to write to it BitmapData bmpData = bmp.LockBits( sourceRect.ToDrawingRectangle(), ImageLockMode.ReadWrite, pxFormat); { // Copy the source bitmap data to our new Bitmap source.CopyPixels(sourceRect, bmpData.Scan0, bmpData.Stride * sourceRect.Height, bmpData.Stride); // The drag image seems to work in full 32-bit color, except when // alpha equals zero. Then it renders those pixels at black. So // we make a pass and set all those pixels to the transparency key // color. This is only implemented for 32-bit pixel colors for now. if ((pxFormat & DrawingPixelFormat.Alpha) == DrawingPixelFormat.Alpha) { ReplaceTransparentPixelsWithTransparentKey(bmpData, transKey); } } // Done, unlock the bits bmp.UnlockBits(bmpData); return(bmp); }
/// <summary> /// Gets a System.Drawing.Bitmap from a BitmapSource. /// </summary> /// <param name="source">The source image from which to create our Bitmap.</param> /// <param name="transparencyKey">The transparency key. This is used by the DragDropHelper /// in rendering transparent pixels.</param> /// <returns>An instance of Bitmap which is a copy of the BitmapSource's image.</returns> private static Bitmap GetBitmapFromBitmapSource(BitmapSource source, Color transparencyKey) { // Copy at full size Int32Rect sourceRect = new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight); // Convert to our destination pixel format DrawingPixelFormat pxFormat = ConvertPixelFormat(source.Format); // Create the Bitmap, full size, full rez Bitmap bmp = new Bitmap(sourceRect.Width, sourceRect.Height, pxFormat); // If the format is an indexed format, copy the color palette if ((pxFormat & DrawingPixelFormat.Indexed) == DrawingPixelFormat.Indexed) ConvertColorPalette(bmp.Palette, source.Palette); // Get the transparency key as a System.Drawing.Color DrawingColor transKey = transparencyKey.ToDrawingColor(); // Lock our Bitmap bits, we need to write to it BitmapData bmpData = bmp.LockBits( sourceRect.ToDrawingRectangle(), ImageLockMode.ReadWrite, pxFormat); { // Copy the source bitmap data to our new Bitmap source.CopyPixels(sourceRect, bmpData.Scan0, bmpData.Stride * sourceRect.Height, bmpData.Stride); // The drag image seems to work in full 32-bit color, except when // alpha equals zero. Then it renders those pixels at black. So // we make a pass and set all those pixels to the transparency key // color. This is only implemented for 32-bit pixel colors for now. if ((pxFormat & DrawingPixelFormat.Alpha) == DrawingPixelFormat.Alpha) ReplaceTransparentPixelsWithTransparentKey(bmpData, transKey); } // Done, unlock the bits bmp.UnlockBits(bmpData); return bmp; }