public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj) { IntPtr intPtr = NativeMethods._SelectObjectSafeHBITMAP(hdc, hgdiobj); if (intPtr == IntPtr.Zero) { HRESULT.ThrowLastError(); } return(intPtr); }
public void CreateDIBitmapTest() { var bits = new byte[128 * 4]; byte[] rlebits = { 2, 0, 0, 0, 2, 1, 0, 1 }; using SafeHDC hdc = GetDC(); BITMAPINFO bmi = new(); bmi.bmiHeader.biSize = (uint)Marshal.SizeOf <BITMAPINFOHEADER>(); bmi.bmiHeader.biWidth = 2; bmi.bmiHeader.biHeight = 2; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 16; bmi.bmiHeader.biCompression = BitmapCompressionMode.BI_RGB; bmi.bmiHeader.biSizeImage = 0; bmi.bmiHeader.biXPelsPerMeter = 1; bmi.bmiHeader.biYPelsPerMeter = 1; bmi.bmiHeader.biClrUsed = 0; bmi.bmiHeader.biClrImportant = 0; SafeHBITMAP hbmp = CreateDIBitmap(hdc, bmi.bmiHeader, CBM.CBM_INIT, bits, bmi, DIBColorMode.DIB_RGB_COLORS); Assert.That(hbmp, ResultIs.ValidHandle); BITMAP bitmap = GetObject <BITMAP>(hbmp); Assert.IsTrue(bitmap.bmType == 0); Assert.IsTrue(bitmap.bmWidth == 2); Assert.IsTrue(bitmap.bmHeight == 2); Assert.That(bitmap.bmWidthBytes, Is.EqualTo(8)); Assert.IsTrue(bitmap.bmPlanes == 1); Assert.IsTrue(bitmap.bmBitsPixel == GetDeviceCaps(hdc, DeviceCap.BITSPIXEL)); Assert.IsTrue(bitmap.bmBits == IntPtr.Zero); bmi.bmiHeader.biCompression = BitmapCompressionMode.BI_RLE8; bmi.bmiHeader.biBitCount = 8; bmi.bmiHeader.biSizeImage = 8; bmi.bmiHeader.biClrUsed = 1; Assert.That(hbmp = CreateDIBitmap(hdc, bmi.bmiHeader, CBM.CBM_INIT, rlebits, new SafeBITMAPINFO(bmi), DIBColorMode.DIB_PAL_COLORS), ResultIs.ValidHandle); bitmap = GetObject <BITMAP>(hbmp); Assert.IsTrue(bitmap.bmType == 0); Assert.IsTrue(bitmap.bmWidth == 2); Assert.IsTrue(bitmap.bmHeight == 2); Assert.That(bitmap.bmWidthBytes, Is.EqualTo(8)); Assert.IsTrue(bitmap.bmPlanes == 1); Assert.IsTrue(bitmap.bmBitsPixel == GetDeviceCaps(hdc, DeviceCap.BITSPIXEL)); Assert.IsTrue(bitmap.bmBits == IntPtr.Zero); }
protected virtual Bitmap GetBitmap(SafeResourceId name, Size size) { var hBmp = new SafeHBITMAP(LoadImage(hLib, name, LoadImageType.IMAGE_BITMAP, size.Width, size.Height, LoadImageOptions.LR_DEFAULTCOLOR)); return(!hBmp.IsNull ? hBmp.ToBitmap() : throw new Win32Exception()); }
public HRESULT GetThumbnail(uint cx, out SafeHBITMAP phbmp, out WTS_ALPHATYPE pdwAlpha) { // Retrieve thumbnails of the placeholders on demand by delegating to the thumbnail of the source items. try { using var thumbnailProviderSource = ComReleaserFactory.Create(_itemSrc.BindToHandler <IThumbnailProvider>(default, BHID.BHID_ThumbnailHandler.Guid()));
private static extern IntPtr _SelectObjectSafeHBITMAP(SafeDC hdc, SafeHBITMAP hgdiobj);
public void Show() { _VerifyMutability(); Stream imageStream = null; try { // Try to use the filepath first. If it's not provided or not available, use the embedded resource. if (!string.IsNullOrEmpty(ImageFileName) && File.Exists(ImageFileName)) { try { imageStream = new FileStream(ImageFileName, FileMode.Open); } catch (IOException) { } } if (imageStream == null) { imageStream = _resourceManager.GetStream(ResourceName, CultureInfo.CurrentUICulture); if (imageStream == null) { throw new IOException("The resource could not be found."); } } Size bitmapSize; _hBitmap = _CreateHBITMAPFromImageStream(imageStream, out bitmapSize); Point location = new Point( (NativeMethods.GetSystemMetrics(SM.CXSCREEN) - bitmapSize.Width) / 2, (NativeMethods.GetSystemMetrics(SM.CYSCREEN) - bitmapSize.Height) / 2); // Pass a null WndProc. Let the MessageWindow use DefWindowProc. _hwndWrapper = new MessageWindow( CS.HREDRAW | CS.VREDRAW, WS.POPUP | WS.VISIBLE, WS_EX.WINDOWEDGE | WS_EX.TOOLWINDOW | WS_EX.LAYERED | (IsTopMost ? WS_EX.TOPMOST : 0), new Rect(location, bitmapSize), "Splash Screen", null); byte opacity = (byte)(FadeInDuration > TimeSpan.Zero ? 0 : 255); using (SafeDC hScreenDC = SafeDC.GetDesktop()) { using (SafeDC memDC = SafeDC.CreateCompatibleDC(hScreenDC)) { IntPtr hOldBitmap = NativeMethods.SelectObject(memDC, _hBitmap); RECT hwndRect = NativeMethods.GetWindowRect(_hwndWrapper.Handle); POINT hwndPos = hwndRect.Position; SIZE hwndSize = hwndRect.Size; POINT origin = new POINT(); BLENDFUNCTION bf = _BaseBlendFunction; bf.SourceConstantAlpha = opacity; NativeMethods.UpdateLayeredWindow(_hwndWrapper.Handle, hScreenDC, ref hwndPos, ref hwndSize, memDC, ref origin, 0, ref bf, ULW.ALPHA); NativeMethods.SelectObject(memDC, hOldBitmap); } } if (CloseOnMainWindowCreation) { Dispatcher.CurrentDispatcher.BeginInvoke( DispatcherPriority.Loaded, (DispatcherOperationCallback)delegate(object splashObj) { var splashScreen = (SplashScreen)splashObj; if (!splashScreen._isClosed) { splashScreen.Close(); } return null; }, this); } _dispatcher = Dispatcher.CurrentDispatcher; if (FadeInDuration > TimeSpan.Zero) { _fadeInEnd = DateTime.UtcNow + FadeInDuration; _dt = new DispatcherTimer(FadeInDuration, DispatcherPriority.Normal, _FadeInTick, _dispatcher); _dt.Start(); } } finally { Utility.SafeDispose(ref imageStream); } }
public void Show() { _VerifyMutability(); Stream imageStream = null; try { // Try to use the filepath first. If it's not provided or not available, use the embedded resource. if (!string.IsNullOrEmpty(ImageFileName) && File.Exists(ImageFileName)) { try { imageStream = new FileStream(ImageFileName, FileMode.Open); } catch (IOException) { } } if (imageStream == null) { imageStream = _resourceManager.GetStream(ResourceName, CultureInfo.CurrentUICulture); if (imageStream == null) { throw new IOException("The resource could not be found."); } } Size bitmapSize; _hBitmap = _CreateHBITMAPFromImageStream(imageStream, out bitmapSize); Point location = new Point( (NativeMethods.GetSystemMetrics(SM.CXSCREEN) - bitmapSize.Width) / 2, (NativeMethods.GetSystemMetrics(SM.CYSCREEN) - bitmapSize.Height) / 2); // Pass a null WndProc. Let the MessageWindow use DefWindowProc. _hwndWrapper = new MessageWindow( CS.HREDRAW | CS.VREDRAW, WS.POPUP | WS.VISIBLE, WS_EX.WINDOWEDGE | WS_EX.TOOLWINDOW | WS_EX.LAYERED | (IsTopMost ? WS_EX.TOPMOST : 0), new Rect(location, bitmapSize), "Splash Screen", null); byte opacity = (byte)(FadeInDuration > TimeSpan.Zero ? 0 : 255); using (SafeDC hScreenDC = SafeDC.GetDesktop()) { using (SafeDC memDC = SafeDC.CreateCompatibleDC(hScreenDC)) { IntPtr hOldBitmap = NativeMethods.SelectObject(memDC, _hBitmap); RECT hwndRect = NativeMethods.GetWindowRect(_hwndWrapper.Handle); POINT hwndPos = hwndRect.Position; SIZE hwndSize = hwndRect.Size; POINT origin = new POINT(); BLENDFUNCTION bf = _BaseBlendFunction; bf.SourceConstantAlpha = opacity; NativeMethods.UpdateLayeredWindow(_hwndWrapper.Handle, hScreenDC, ref hwndPos, ref hwndSize, memDC, ref origin, 0, ref bf, ULW.ALPHA); NativeMethods.SelectObject(memDC, hOldBitmap); } } if (CloseOnMainWindowCreation) { Dispatcher.CurrentDispatcher.BeginInvoke( DispatcherPriority.Loaded, (DispatcherOperationCallback) delegate(object splashObj) { var splashScreen = (SplashScreen)splashObj; if (!splashScreen._isClosed) { splashScreen.Close(); } return(null); }, this); } _dispatcher = Dispatcher.CurrentDispatcher; if (FadeInDuration > TimeSpan.Zero) { _fadeInEnd = DateTime.UtcNow + FadeInDuration; _dt = new DispatcherTimer(FadeInDuration, DispatcherPriority.Normal, _FadeInTick, _dispatcher); _dt.Start(); } } finally { Utility.SafeDispose(ref imageStream); } }
private static SafeHBITMAP _CreateHBITMAPFromImageStream(Stream imgStream, out Size bitmapSize) { IWICImagingFactory pImagingFactory = null; IWICBitmapDecoder pDecoder = null; IWICStream pStream = null; IWICBitmapFrameDecode pDecodedFrame = null; IWICFormatConverter pBitmapSourceFormatConverter = null; IWICBitmapFlipRotator pBitmapFlipRotator = null; SafeHBITMAP hbmp = null; try { using (var istm = new ManagedIStream(imgStream)) { pImagingFactory = CLSID.CoCreateInstance <IWICImagingFactory>(CLSID.WICImagingFactory); pStream = pImagingFactory.CreateStream(); pStream.InitializeFromIStream(istm); // Create an object that will decode the encoded image Guid vendor = Guid.Empty; pDecoder = pImagingFactory.CreateDecoderFromStream(pStream, ref vendor, WICDecodeMetadata.CacheOnDemand); pDecodedFrame = pDecoder.GetFrame(0); pBitmapSourceFormatConverter = pImagingFactory.CreateFormatConverter(); // Convert the image from whatever format it is in to 32bpp premultiplied alpha BGRA Guid pixelFormat = WICPixelFormat.WICPixelFormat32bppPBGRA; pBitmapSourceFormatConverter.Initialize(pDecodedFrame, ref pixelFormat, WICBitmapDitherType.None, IntPtr.Zero, 0, WICBitmapPaletteType.Custom); pBitmapFlipRotator = pImagingFactory.CreateBitmapFlipRotator(); pBitmapFlipRotator.Initialize(pBitmapSourceFormatConverter, WICBitmapTransform.FlipVertical); int width, height; pBitmapFlipRotator.GetSize(out width, out height); bitmapSize = new Size { Width = width, Height = height }; var bmi = new BITMAPINFO { bmiHeader = new BITMAPINFOHEADER { biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)), biWidth = width, biHeight = height, biPlanes = 1, biBitCount = 32, biCompression = BI.RGB, biSizeImage = (width * height * 4), }, }; // Create a 32bpp DIB. This DIB must have an alpha channel for UpdateLayeredWindow to succeed. IntPtr pBitmapBits; hbmp = NativeMethods.CreateDIBSection(null, ref bmi, out pBitmapBits, IntPtr.Zero, 0); // Copy the decoded image to the new buffer which backs the HBITMAP var rect = new WICRect { X = 0, Y = 0, Width = width, Height = height }; pBitmapFlipRotator.CopyPixels(ref rect, width * 4, bmi.bmiHeader.biSizeImage, pBitmapBits); var ret = hbmp; hbmp = null; return(ret); } } finally { Utility.SafeRelease(ref pImagingFactory); Utility.SafeRelease(ref pDecoder); Utility.SafeRelease(ref pStream); Utility.SafeRelease(ref pDecodedFrame); Utility.SafeRelease(ref pBitmapFlipRotator); Utility.SafeRelease(ref pBitmapSourceFormatConverter); Utility.SafeDispose(ref hbmp); } }