예제 #1
0
        public Color GetPixel(int x, int y)
        {
            if (x < 0 || x > NativeCGImage.Width - 1)
            {
                throw new InvalidEnumArgumentException("Parameter must be positive and < Width.");
            }
            if (y < 0 || y > NativeCGImage.Height - 1)
            {
                throw new InvalidEnumArgumentException("Parameter must be positive and < Height.");
            }

            // We are going to cheat here and instead of reading the bytes of the original image
            // parsing from there a pixel and converting to a format we will just create
            // a 1 x 1 image of the pixel that we want.  I am supposing this should be really
            // fast.
            var pixelImage = NativeCGImage.WithImageInRect(new CGRect(x, y, 1, 1));

            var pData = pixelImage.DataProvider;
            var nData = pData.CopyData();

            // We may have to parse out the bytes with 4 or 3 bytes per pixel later.
            var pixelColor = Color.FromArgb(nData[3], nData[0], nData[1], nData[2]);

            pixelImage.Dispose();

            return(pixelColor);
        }
예제 #2
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (NativeCGImage != null)
         {
             NativeCGImage.Dispose();
             NativeCGImage = null;
         }
         //Marshal.FreeHGlobal (bitmapBlock);
         bitmapBlock = IntPtr.Zero;
         Console.WriteLine("Bitmap Dispose");
     }
     base.Dispose(disposing);
 }
예제 #3
0
        private void InitializeImageFrame(int frame)
        {
            if (NativeCGImage != null)
            {
                NativeCGImage.Dispose();
            }

            imageTransform = CGAffineTransform.MakeIdentity();

            SetImageInformation(frame);
            var cg = imageSource.CreateImage(frame, null);

            imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, cg.Height);
            InitWithCGImage(cg);
            GuessPixelFormat();

            currentFrame = frame;
        }
예제 #4
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (NativeCGImage != null)
         {
             NativeCGImage.Dispose();
             NativeCGImage = null;
         }
         if (dataProvider != null)
         {
             dataProvider.Dispose();
             dataProvider = null;
         }
         if (imageSource != null)
         {
             imageSource.Dispose();
             imageSource = null;
         }
     }
     base.Dispose(disposing);
 }
예제 #5
0
        internal new void RotateFlip(RotateFlipType rotateFlipType)
        {
            CGAffineTransform rotateFlip = CGAffineTransform.MakeIdentity();

            int width, height;

            width  = (int)NativeCGImage.Width;
            height = (int)NativeCGImage.Height;

            switch (rotateFlipType)
            {
            //			case RotateFlipType.RotateNoneFlipNone:
            //			//case RotateFlipType.Rotate180FlipXY:
            //				rotateFlip = GeomUtilities.CreateRotateFlipTransform (b.Width, b.Height, 0, false, false);
            //				break;
            case RotateFlipType.Rotate90FlipNone:
                //case RotateFlipType.Rotate270FlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 90, false, false);
                break;

            case RotateFlipType.Rotate180FlipNone:
                //case RotateFlipType.RotateNoneFlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 0, true, true);
                break;

            case RotateFlipType.Rotate270FlipNone:
                //case RotateFlipType.Rotate90FlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 270, false, false);
                break;

            case RotateFlipType.RotateNoneFlipX:
                //case RotateFlipType.Rotate180FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 0, true, false);
                break;

            case RotateFlipType.Rotate90FlipX:
                //case RotateFlipType.Rotate270FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 90, true, false);
                break;

            case RotateFlipType.Rotate180FlipX:
                //case RotateFlipType.RotateNoneFlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 0, false, true);
                break;

            case RotateFlipType.Rotate270FlipX:
                //case RotateFlipType.Rotate90FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 270, true, false);
                break;
            }

            var bytesPerRow      = (width * (int)NativeCGImage.BitsPerPixel + 7) / 8;
            var newBitmapBlock   = Marshal.AllocHGlobal(height * bytesPerRow);
            var newBitmapContext = new CGBitmapContext(newBitmapBlock, width, height, NativeCGImage.BitsPerComponent, bytesPerRow, NativeCGImage.ColorSpace, NativeCGImage.AlphaInfo);

            newBitmapContext.ConcatCTM(rotateFlip);
            newBitmapContext.DrawImage(new CGRect(0, 0, NativeCGImage.Width, NativeCGImage.Height), NativeCGImage);
            newBitmapContext.Flush();

            // If the width or height is not the seme we need to switch the dpiHeight and dpiWidth
            // We should be able to get around this with set resolution later.
            if (NativeCGImage.Width != width || NativeCGImage.Height != height)
            {
                var temp = dpiWidth;
                dpiHeight = dpiWidth;
                dpiWidth  = temp;
            }

            physicalDimension.Width  = (float)width;
            physicalDimension.Height = (float)height;

            physicalSize         = new SizeF(physicalDimension.Width, physicalDimension.Height);
            physicalSize.Width  *= ConversionHelpers.MS_DPI / dpiWidth;
            physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight;

            // In windows the RawFormat is changed to MemoryBmp to show that the image has changed.
            rawFormat = ImageFormat.MemoryBmp;

            // Set our transform for this image for the new height
            imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, height);

            // bitmapBlock is owned by dataProvider and freed implicitly
            if (dataProvider != null)
            {
                dataProvider.Dispose();
            }

            if (cachedContext != null)
            {
                cachedContext.Dispose();
            }
            NativeCGImage.Dispose();

            this.bitmapBlock   = newBitmapBlock;
            this.dataProvider  = new CGDataProvider(bitmapBlock, height * bytesPerRow);
            this.NativeCGImage = newBitmapContext.ToImage();
            this.cachedContext = newBitmapContext;
            this.imageSource   = null;

            // update the cached size
            imageSize.Width  = this.Width;
            imageSize.Height = this.Height;
        }