Exemplo n.º 1
0
        private PixelFormat ConvertPixelFormat(System.Drawing.Imaging.PixelFormat sourceFormat)
        {
            switch (sourceFormat)
            {
            case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                return(PixelFormats.Bgr24);

            case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                return(PixelFormats.Pbgra32);

            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                return(PixelFormats.Bgra32);

            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                return(PixelFormats.Bgr32);

            case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
                return(PixelFormats.Bgr555);

            case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
                return(PixelFormats.Bgr565);

            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                return(PixelFormats.Indexed8);

                // .. add more if needed
            }

            return(new PixelFormat());
        }
Exemplo n.º 2
0
        private static PixelFormat ConvertBmpPixelFormat(System.Drawing.Imaging.PixelFormat pixelformat)
        {
            var pixelFormats = PixelFormats.Default;

            switch (pixelformat)
            {
            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                pixelFormats = PixelFormats.Bgra32;
                break;

            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                pixelFormats = PixelFormats.Gray8;
                break;

            case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
                pixelFormats = PixelFormats.Gray16;
                break;

            case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
                pixelFormats = PixelFormats.Bgr555;
                break;
            }

            return(pixelFormats);
        }
Exemplo n.º 3
0
        private static PixelFormat ConvertPixelFormat(
            System.Drawing.Imaging.PixelFormat sourceFormat)
        {
            switch (sourceFormat)
            {
            case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                return(PixelFormats.Bgr24);

            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                return(PixelFormats.Bgra32);

            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                return(PixelFormats.Bgr32);
            }
            return(new PixelFormat());
        }
        /// <summary>
        /// Конвертация Bitmap в другой формат Bitmap.
        /// </summary>
        /// <param name="source">Источник для конвертации.</param>
        /// <param name="destinationFormat">Новый формат.</param>
        /// <param name="destinationPalette">
        /// Палитра для нового формата, если конечно она нужна для нового формата, иначе передать null.
        /// </param>
        /// <returns>Bitmap в новом формате.</returns>
        public static Bitmap ConvertTo(
            this Bitmap source,
            System.Drawing.Imaging.PixelFormat destinationFormat,
            ColorPalette destinationPalette)
        {
            var result =
                new Bitmap(source.Width, source.Height, destinationFormat);

            if (destinationPalette != null)
            {
                result.Palette = destinationPalette;
            }
            using (Graphics g = Graphics.FromImage(result))
                g.DrawImage(source, 0, 0);
            return(result);
        }
        /// <summary>
        /// Конструктор копирует пиксели из заданного битмапа.
        /// </summary>
        /// <param name="source">Исходный битмап.</param>
        public BitmapWrapperKW(Bitmap source)
        {
            _dpiX   = source.HorizontalResolution;
            _dpiY   = source.VerticalResolution;
            _height = source.Height;
            _width  = source.Width;

            System.Drawing.Imaging.PixelFormat pf = source.PixelFormat;

            if (pf != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            {
                source = source.ConvertTo(System.Drawing.Imaging.PixelFormat.Format32bppArgb, null);
            }

            _pixels = new byte[_width * _height * 4];

            BitmapData bmData = source.LockBits(
                new Rectangle(0, 0, _width, _height),
                ImageLockMode.ReadWrite,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb
                );

            int    strideBm = bmData.Stride;
            IntPtr scan     = bmData.Scan0;

            //TODO: сделать без unsafe
            unsafe
            {
                var p      = (byte *)(void *)scan;
                int offset = 0;
                for (int j = 0; j < _height; j++)
                {
                    int offsetBm = strideBm * j;
                    for (int i = 0; i < _width; i++)
                    {
                        _pixels[offset++] = p[offsetBm++]; //blue
                        _pixels[offset++] = p[offsetBm++]; //green
                        _pixels[offset++] = p[offsetBm++]; //red
                        _pixels[offset++] = p[offsetBm++]; //alfa
                    }
                }
            }

            source.UnlockBits(bmData);
        }