Пример #1
0
        private static LOGFONT CreatePointFontStruct(string fontName, int pointSize, bool bold, bool italic)
        {
            const byte DEFAULT_CHARSET = 1;
            const long FW_BOLD         = 700;
            const int  LOGPIXELSY      = 90;

            LOGFONT font = new LOGFONT();

            font.lfCharSet  = DEFAULT_CHARSET;
            font.lfFaceName = fontName;
            if (bold)
            {
                font.lfWeight = FW_BOLD;
            }
            if (italic)
            {
                font.lfItalic = 1;
            }

            using (GraphicsContext context = GraphicsContext.CreateOffscreenContext())
            {
                font.lfHeight = -NativeMethods.MulDiv(pointSize, context.GetCapability(LOGPIXELSY), 72);
            }

            return(font);
        }
Пример #2
0
        public Bitmap Duplicate()
        {
            // Technique taken from here: http://stackoverflow.com/questions/5687263/copying-a-bitmap-from-another-hbitmap
            using (var sourceContext = GraphicsContext.CreateOffscreenContext())
            {
                using (var destContext = sourceContext.CreateCompatibleContext())
                {
                    const int SRCCOPY = 0x00CC0020;
                    var       header  = Header;

                    sourceContext.CurrentBitmap = this;
                    NativeMethods.BitBlt(sourceContext.Handle, 0, 0, header.bmWidth, header.bmHeight, destContext.Handle, 0, 0, SRCCOPY);
                    return(destContext.CreateBitmap(header.bmWidth, header.bmHeight));
                }
            }
        }
Пример #3
0
        private static Bitmap LoadImageFromBitmapSource(NativeMethods.IWICBitmapSource source)
        {
            int width, height;

            source.GetSize(out width, out height);

            BITMAPINFO info = new BITMAPINFO();

            info.biSize        = Marshal.SizeOf <BITMAPINFOHEADER>();
            info.biWidth       = width;
            info.biHeight      = -height; // the negation is required here
            info.biPlanes      = 1;
            info.biBitCount    = 32;
            info.biCompression = 0; // == BL_RGB

            using (GraphicsContext context = GraphicsContext.CreateOffscreenContext())
            {
                IntPtr imageBits = IntPtr.Zero;
                IntPtr hBitmap   = NativeMethods.CreateDIBSection(context.Handle, ref info, 0, out imageBits, IntPtr.Zero, 0);
                if (hBitmap == IntPtr.Zero)
                {
                    throw new System.ComponentModel.Win32Exception();
                }

                int stride  = width * 4;
                int cbImage = stride * height;

                try
                {
                    source.CopyPixels(IntPtr.Zero, stride, cbImage, imageBits);
                    return(new Bitmap(hBitmap));
                }
                catch
                {
                    NativeMethods.DeleteObject(hBitmap);
                    throw;
                }
            }
        }