public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj) { IntPtr intPtr = NativeMethods._SelectObjectSafeHBITMAP(hdc, hgdiobj); if (intPtr == IntPtr.Zero) { HRESULT.ThrowLastError(); } return(intPtr); }
public static SafeHBITMAP CreateDIBSection(SafeDC hdc, ref BITMAPINFO bitmapInfo, out IntPtr ppvBits, IntPtr hSection, int dwOffset) { SafeHBITMAP safeHBITMAP = null; safeHBITMAP = (hdc != null ? Standard.NativeMethods.CreateDIBSection_1(hdc, ref bitmapInfo, 0, out ppvBits, hSection, dwOffset) : Standard.NativeMethods.CreateDIBSection_2(IntPtr.Zero, ref bitmapInfo, 0, out ppvBits, hSection, dwOffset)); if (safeHBITMAP.IsInvalid) { HRESULT.ThrowLastError(); } return(safeHBITMAP); }
public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj) { IntPtr ret = _SelectObjectSafeHBITMAP(hdc, hgdiobj); if (ret == IntPtr.Zero) { HRESULT.ThrowLastError(); } return ret; }
private static extern IntPtr _SelectObjectSafeHBITMAP(SafeDC hdc, SafeHBITMAP hgdiobj);
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); } }
public void Show() { _VerifyMutability(); using (Stream imageStream = _GetImageStream()) { 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(); } } }