public override NSImage GetImage() { var copy = new BitmapHandler(); copy.Create(size.Width, size.Height, PixelFormat.Format32bppRgb); CopyTo(copy, new Rectangle(size)); return(copy.Control); }
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); }
void CopyTo(BitmapHandler bmp, Rectangle source) { var bd = bmp.Lock(); 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 unsafe { /*fixed (byte* pSrc = Control)*/ { var dest = (byte *)bd.Data; //var src = pSrc; var src = (byte *)ptr; var scany = size.Width; /*if (false) * { * src += Control.Length - scany; * scany = -scany; * }*/ dest += source.Top * bd.ScanWidth; dest += source.Left * sizeof(uint); src += source.Top * scany; src += source.Left; int bottom = source.Bottom; int right = source.Right; int left = source.Left; for (int y = source.Top; y < bottom; y++) { var srcrow = src; var destrow = (uint *)dest; for (int x = left; x < right; x++) { * destrow = colors [*srcrow]; destrow++; srcrow++; } dest += bd.ScanWidth; src += scany; } } } bmp.Unlock(bd); }
public void Create(int width, int height, int bitsPerPixel) { bytesPerRow = width * bitsPerPixel / 8; int colorCount = (int)Math.Pow(2, bitsPerPixel); colors = new uint[colorCount]; for (int i = 0; i < colorCount; i++) { colors [i] = 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); }
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 = (uint *)dest; for (int x = left; x < right; x++) { *destrow = colors[*src]; destrow++; src++; } dest += bd.ScanWidth; src += scany; } } bmp.Unlock(bd); }