Пример #1
0
        public override NSImage GetImage()
        {
            var copy = new BitmapHandler();

            copy.Create(size.Width, size.Height, PixelFormat.Format32bppRgb);
            CopyTo(copy, new Rectangle(size));
            return(copy.Control);
        }
Пример #2
0
 protected override void Dispose(bool disposing)
 {
     if (disposing && bmp != null)
     {
         bmp.Dispose();
         bmp = null;
     }
     if (ptr != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(ptr);
         ptr = IntPtr.Zero;
     }
     base.Dispose(disposing);
 }
Пример #3
0
        public void Create(int width, int height, int bitsPerPixel)
        {
            this.bitsPerPixel = bitsPerPixel;
            bytesPerRow       = width * bitsPerPixel / 8;
            int colorCount = (int)Math.Pow(2, bitsPerPixel);

            colors = new int[colorCount];
            for (int i = 0; i < colorCount; i++)
            {
                colors[i] = unchecked ((int)0xffffffff);
            }

            size = new Size(width, height);
            ptr  = Marshal.AllocHGlobal(height * bytesPerRow);
            //Control = new byte[height * bytesPerRow];
            bmp = new BitmapHandler();
            bmp.Create(size.Width, size.Height, PixelFormat.Format32bppRgb);
        }
Пример #4
0
        void CopyTo(BitmapHandler bmp, Rectangle source)
        {
            if (source.Top < 0 || source.Left < 0 || source.Right > size.Width || source.Bottom > size.Height)
            {
                throw new Exception("Source rectangle exceeds image size");
            }

            // we have to draw to a temporary bitmap pixel by pixel
            var bd = bmp.Lock();

            unsafe
            {
                var dest  = (byte *)bd.Data;
                var src   = (byte *)ptr;
                var scany = size.Width;

                dest += source.Top * bd.ScanWidth;
                dest += source.Left * bd.BytesPerPixel;

                src += source.Top * scany;
                src += source.Left;

                int bottom = source.Bottom;
                int right  = source.Right;
                int left   = source.Left;
                scany = scany - (right - left);
                for (int y = source.Top; y < bottom; y++)
                {
                    var destrow = (int *)dest;
                    for (int x = left; x < right; x++)
                    {
                        *destrow = colors[*src];
                        destrow++;
                        src++;
                    }
                    dest += bd.ScanWidth;
                    src  += scany;
                }
            }
            bmp.Unlock(bd);
        }