当前位置: 首页>>代码示例>>C#>>正文


C# CGBitmapContext.DrawImage方法代码示例

本文整理汇总了C#中CGBitmapContext.DrawImage方法的典型用法代码示例。如果您正苦于以下问题:C# CGBitmapContext.DrawImage方法的具体用法?C# CGBitmapContext.DrawImage怎么用?C# CGBitmapContext.DrawImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CGBitmapContext的用法示例。


在下文中一共展示了CGBitmapContext.DrawImage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Bitmap

        public Bitmap(NSImage sourceImage)
        {
            Width = (int)sourceImage.CGImage.Width;
            Height = (int)sourceImage.CGImage.Height;
            _colors = new Color[Width * Height];

            var rawData = new byte[Width * Height * 4];
            var bytesPerPixel = 4;
            var bytesPerRow = bytesPerPixel * Width;
            var bitsPerComponent = 8;

            using (var colorSpace = CGColorSpace.CreateDeviceRGB())
            {
                using (var context = new CGBitmapContext(rawData, Width, Height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast))
                {
                    context.DrawImage(new CGRect(0, 0, Width, Height), sourceImage.CGImage);

                    for (int y = 0; y < Height; y++)
                    {
                        for (int x = 0; x < Width; x++)
                        {
                            var i = bytesPerRow * y + bytesPerPixel * x;   
                            byte red = rawData[i + 0];
                            byte green = rawData[i + 1];
                            byte blue = rawData[i + 2];
                            byte alpha = rawData[i + 3];
                            SetPixel(x, y, Color.FromArgb(alpha, red, green, blue));
                        }
                    }
                }
            }
        }
开发者ID:SubtitleEdit,项目名称:subtitleedit-mac,代码行数:32,代码来源:Bitmap.cs

示例2: ResizeImageIOS

		public byte[] ResizeImageIOS(byte[] imageData, float size)
		{
			UIImage originalImage = ImageFromByteArray(imageData);
			System.Diagnostics.Debug.Write("originalImage.Size.Height"+ originalImage.Size.Height + ", " + originalImage.Size.Width);
			UIImageOrientation orientation = originalImage.Orientation;

			float width = size;
			float height = ((float)originalImage.Size.Height / (float)originalImage.Size.Width) * size;
			System.Diagnostics.Debug.Write("new size" + width + ", " + height);
			//create a 24bit RGB image
			using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero,
												 (int)width, (int)height, 8,
												 (int)(4 * width), CGColorSpace.CreateDeviceRGB(),
												 CGImageAlphaInfo.PremultipliedFirst))
			{

				RectangleF imageRect = new RectangleF(0, 0, width, height);

				// draw the image
				context.DrawImage(imageRect, originalImage.CGImage);

				UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation);

				// save the image as a jpeg
				return resizedImage.AsJPEG().ToArray();
			}
		}
开发者ID:todibbang,项目名称:HowlOutApp,代码行数:27,代码来源:ImageResizeRenderer.cs

示例3: iOSSurfaceSource

        /// <summary>
        /// Initializes a new instance of the <see cref="iOSSurfaceSource"/> class.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> that contains the surface data.</param>
        public iOSSurfaceSource(Stream stream)
        {
            Contract.Require(stream, nameof(stream));

            using (var data = NSData.FromStream(stream))
            {
                using (var img = UIImage.LoadFromData(data))
                {
                    this.width = (Int32)img.Size.Width;
                    this.height = (Int32)img.Size.Height;
                    this.stride = (Int32)img.CGImage.BytesPerRow;

                    this.bmpData = Marshal.AllocHGlobal(stride * height);

                    using (var colorSpace = CGColorSpace.CreateDeviceRGB())
                    {
                        using (var bmp = new CGBitmapContext(bmpData, width, height, 8, stride, colorSpace, CGImageAlphaInfo.PremultipliedLast))
                        {
                            bmp.ClearRect(new CGRect(0, 0, width, height));
                            bmp.DrawImage(new CGRect(0, 0, width, height), img.CGImage);
                        }
                    }
                }
            }

            ReversePremultiplication();
        }
开发者ID:RUSshy,项目名称:ultraviolet,代码行数:31,代码来源:iOSSurfaceSource.cs

示例4: GetImagaDataFromPath

                void GetImagaDataFromPath (string path)
                {
                        int width, height;
                        NSImage src;
                        CGImage image;
                        CGContext context = null;
                        
                        data = new byte[TEXTURE_WIDTH * TEXTURE_HEIGHT * 4];
                        
                        src = new NSImage (path);
						var rect = CGRect.Empty;

                        image = src.AsCGImage (ref rect, null, null);
			            width = (int) image.Width;
			            height = (int)image.Height;
                        
                        CGImageAlphaInfo ai = CGImageAlphaInfo.PremultipliedLast;
                        
                        context = new CGBitmapContext (data, width, height, 8, 4 * width, image.ColorSpace, ai);
                        
                        // Core Graphics referential is upside-down compared to OpenGL referential
                        // Flip the Core Graphics context here
                        // An alternative is to use flipped OpenGL texture coordinates when drawing textures
                        context.TranslateCTM (0, height);
                        context.ScaleCTM (1, -1);
                        
                        // Set the blend mode to copy before drawing since the previous contents of memory aren't used. 
                        // This avoids unnecessary blending.
                        context.SetBlendMode (CGBlendMode.Copy);
                        
                        context.DrawImage (new CGRect (0, 0, width, height), image);
                }
开发者ID:RangoLee,项目名称:mac-samples,代码行数:32,代码来源:Texture.cs

示例5: ViewDidLoad

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
			// set the background color of the view to white
			View.BackgroundColor = UIColor.White;
			
			// instantiate a new image view that takes up the whole screen and add it to 
			// the view hierarchy
			RectangleF imageViewFrame = new RectangleF (0, -NavigationController.NavigationBar.Frame.Height, View.Frame.Width, View.Frame.Height);
			imageView = new UIImageView (imageViewFrame);
			View.AddSubview (imageView);
			
			// create our offscreen bitmap context
			// size
			SizeF bitmapSize = new SizeF (View.Frame.Size);
			using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero
					, (int)bitmapSize.Width, (int)bitmapSize.Height, 8
					, (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB ()
					, CGImageAlphaInfo.PremultipliedFirst)) {
				
				// declare vars
				UIImage apressImage = UIImage.FromFile ("Images/Icons/512_icon.png");
				PointF imageOrigin = new PointF ((imageView.Frame.Width / 2) - (apressImage.CGImage.Width / 2), (imageView.Frame.Height / 2) - (apressImage.CGImage.Height / 2)); 
				RectangleF imageRect = new RectangleF (imageOrigin.X, imageOrigin.Y, apressImage.CGImage.Width, apressImage.CGImage.Height);
				
				// draw the image
				context.DrawImage (imageRect, apressImage.CGImage);
				
				
				// output the drawing to the view
				imageView.Image = UIImage.FromImage (context.ToImage ());
			}
		}
开发者ID:rojepp,项目名称:monotouch-samples,代码行数:34,代码来源:Controller.cs

示例6: GetPixelColor

        private UIColor GetPixelColor(CGPoint point, UIImage image)
        {
            var rawData = new byte[4];
            var handle = GCHandle.Alloc(rawData);
            UIColor resultColor = null;

            try
            {
                using (var colorSpace = CGColorSpace.CreateDeviceRGB())
                {
                    using (var context = new CGBitmapContext(rawData, 1, 1, 8, 4, colorSpace, CGImageAlphaInfo.PremultipliedLast))
                    {
                        context.DrawImage(new CGRect(-point.X, point.Y - image.Size.Height, image.Size.Width, image.Size.Height), image.CGImage);

                        float red   = (rawData[0]) / 255.0f;
                        float green = (rawData[1]) / 255.0f;
                        float blue  = (rawData[2]) / 255.0f;
                        float alpha = (rawData[3]) / 255.0f;

                        resultColor = UIColor.FromRGBA(red, green, blue, alpha);
                    }
                }
            }
            finally
            {
                handle.Free();
            }

            return resultColor;
        }
开发者ID:Manne990,项目名称:XamTest,代码行数:30,代码来源:HexagonButtonViewRenderer.cs

示例7: LoadBitmapData

		void LoadBitmapData (int texId)
		{
			NSData texData = NSData.FromFile (NSBundle.MainBundle.PathForResource ("texture1", "png"));

			UIImage image = UIImage.LoadFromData (texData);
			if (image == null)
				return;

			int width = image.CGImage.Width;
			int height = image.CGImage.Height;

			CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ();
			byte[] imageData = new byte[height * width * 4];
			CGContext context = new CGBitmapContext (imageData, width, height, 8, 4 * width, colorSpace,
				                    CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);

			context.TranslateCTM (0, height);
			context.ScaleCTM (1, -1);
			colorSpace.Dispose ();
			context.ClearRect (new RectangleF (0, 0, width, height));
			context.DrawImage (new RectangleF (0, 0, width, height), image.CGImage);

			GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData);
			context.Dispose ();
		}
开发者ID:Juliansanu,项目名称:mobile-samples,代码行数:25,代码来源:EAGLView.cs

示例8: ResizeImage

        public byte[] ResizeImage(byte[] imageData, float width, float height)
        {
            UIImage originalImage = ImageFromByteArray (imageData);
            float oldWidth = (float)originalImage.Size.Width;
            float oldHeight = (float)originalImage.Size.Height;
            float scaleFactor = 0f;

            if (oldWidth > oldHeight) {
                scaleFactor = width / oldWidth;
            } else {
                scaleFactor = height / oldHeight;
            }
            float newHeight = oldHeight * scaleFactor;
            float newWidth = oldWidth * scaleFactor;

            //create a 24bit RGB image
            using (CGBitmapContext context = new CGBitmapContext (null,
                                                 (int)newWidth, (int)newHeight, 8,
                                                 0, CGColorSpace.CreateDeviceRGB (), CGImageAlphaInfo.PremultipliedFirst)) {

                RectangleF imageRect = new RectangleF (0, 0, newWidth, newHeight);

                // draw the image
                context.DrawImage (imageRect, originalImage.CGImage);

                UIKit.UIImage resizedImage = UIKit.UIImage.FromImage (context.ToImage ());

                // save the image as a jpeg
                return resizedImage.AsJPEG ().ToArray ();
            }
        }
开发者ID:BorisFR,项目名称:astromech,代码行数:31,代码来源:ImageResizer.cs

示例9: CalculateLuminance

      private void CalculateLuminance(UIImage d)
      {
         var imageRef = d.CGImage;
         var width = imageRef.Width;
         var height = imageRef.Height;
         var colorSpace = CGColorSpace.CreateDeviceRGB();

         var rawData = Marshal.AllocHGlobal(height * width * 4);

         try
         {
			var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little; 
            var context = new CGBitmapContext(rawData, width, height, 8, 4 * width,
            colorSpace, (CGImageAlphaInfo)flags);

            context.DrawImage(new RectangleF(0.0f, 0.0f, (float)width, (float)height), imageRef);
            var pixelData = new byte[height * width * 4];
            Marshal.Copy(rawData, pixelData, 0, pixelData.Length);

            CalculateLuminance(pixelData, BitmapFormat.BGRA32);
         }
         finally
         {
            Marshal.FreeHGlobal(rawData);
         }
      }
开发者ID:Woo-Long,项目名称:ZXing.Net.Mobile,代码行数:26,代码来源:RGBLuminanceSource.monotouch.cs

示例10: init

        protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                using (var imageData = NSData.FromStream(stream))
                using (var image = UIImage.LoadFromData(imageData))
                {
                    int width = (int)image.Size.Width;
                    int height = (int)image.Size.Height;
                    Mipmaps = new Mipmap[1];
                    Size = new Size2(width, height);

                    var data = new byte[width * height * 4];
                    using (CGContext imageContext = new CGBitmapContext(data, width, height, 8, width*4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast))
                    {
                        imageContext.DrawImage(new RectangleF(0, 0, width, height), image.CGImage);

                        Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
                        if (flip) Mipmaps[0].FlipVertical();
                    }
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                if (loadedCallback != null) loadedCallback(this, false);
                return;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
        }
开发者ID:reignstudios,项目名称:ReignSDK,代码行数:33,代码来源:ImageIOS.cs

示例11: GrayscaleImage

        /// <summary>
        ///    Creates grayscaled image from existing image.
        /// </summary>
        /// <param name="oldImage">Image to convert.</param>
        /// <returns>Returns grayscaled image.</returns>
        public static UIImage GrayscaleImage( UIImage oldImage )
        {
            var imageRect = new RectangleF(PointF.Empty, (SizeF) oldImage.Size);

            CGImage grayImage;

            // Create gray image.
            using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceGray()) {
                using (var context = new CGBitmapContext(IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) {
                    context.DrawImage(imageRect, oldImage.CGImage);
                    grayImage = context.ToImage();
                }
            }

            // Create mask for transparent areas.
            using (var context = new CGBitmapContext(IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, CGColorSpace.Null, CGBitmapFlags.Only)) {
                context.DrawImage(imageRect, oldImage.CGImage);
                CGImage alphaMask = context.ToImage();
                var newImage = new UIImage(grayImage.WithMask(alphaMask));

                grayImage.Dispose();
                alphaMask.Dispose();

                return newImage;
            }
        }
开发者ID:strongloop,项目名称:loopback-example-xamarin,代码行数:31,代码来源:ImageHelper.cs

示例12: ConvertToGrayScale

 UIImage ConvertToGrayScale(UIImage image)
 {
     RectangleF imageRect = new RectangleF (0, 0, (float)image.Size.Width, (float)image.Size.Height);
     using (var colorSpace = CGColorSpace.CreateDeviceGray ())
     using (var context = new CGBitmapContext (IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) {
         context.DrawImage (imageRect, image.CGImage);
         using (var imageRef = context.ToImage ())
             return new UIImage (imageRef);
     }
 }
开发者ID:FangHuaiAn,项目名称:Grayscale-Xamarin,代码行数:10,代码来源:ViewController.cs

示例13: ConvertToGrayScale

 public static UIImage ConvertToGrayScale (UIImage image)
 {
     var imageRect = new CGRect (CGPoint.Empty, image.Size);
     using (var colorSpace = CGColorSpace.CreateDeviceGray ())
     using (var context = new CGBitmapContext (IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) {
         context.DrawImage (imageRect, image.CGImage);
         using (var imageRef = context.ToImage ())
             return new UIImage (imageRef);
     }
 }
开发者ID:runt18,项目名称:CodeHub,代码行数:10,代码来源:UIImageHelper.cs

示例14: ToColorSpace

		public static UIImage ToColorSpace(UIImage source, CGColorSpace colorSpace)
		{
			CGRect bounds = new CGRect(0, 0, source.Size.Width, source.Size.Height);

			using (var context = new CGBitmapContext(IntPtr.Zero, (int)bounds.Width, (int)bounds.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) 
			{
				context.DrawImage(bounds, source.CGImage);
				using (var imageRef = context.ToImage())
				{
					return new UIImage(imageRef);
				}
			}
		}
开发者ID:Sohojoe,项目名称:ImagePickerCropAndResize,代码行数:13,代码来源:ColorSpaceTransformation.cs

示例15: PaintingView

		public PaintingView (RectangleF frame)
			: base (frame)
		{
			LayerRetainsBacking = true;
			LayerColorFormat    = EAGLColorFormat.RGBA8;
			ContextRenderingApi = EAGLRenderingAPI.OpenGLES1;
			CreateFrameBuffer();
			MakeCurrent();

			var brushImage = UIImage.FromFile ("Particle.png").CGImage;
			var width = brushImage.Width;
			var height = brushImage.Height;
			if (brushImage != null) {
				IntPtr brushData = Marshal.AllocHGlobal (width * height * 4);
				if (brushData == IntPtr.Zero)
					throw new OutOfMemoryException ();
				try {
					using (var brushContext = new CGBitmapContext (brushData,
							width, width, 8, width * 4, brushImage.ColorSpace, CGImageAlphaInfo.PremultipliedLast)) {
						brushContext.DrawImage (new RectangleF (0.0f, 0.0f, (float) width, (float) height), brushImage);
					}

					GL.GenTextures (1, ref brushTexture);
					GL.BindTexture (All.Texture2D, brushTexture);
					GL.TexImage2D (All.Texture2D, 0, (int) All.Rgba, width, height, 0, All.Rgba, All.UnsignedByte, brushData);
				}
				finally {
					Marshal.FreeHGlobal (brushData);
				}
				GL.TexParameter (All.Texture2D, All.TextureMinFilter, (int) All.Linear);
				GL.Enable (All.Texture2D);
				GL.BlendFunc (All.SrcAlpha, All.One);
				GL.Enable (All.Blend);
			}
			GL.Disable (All.Dither);
			GL.MatrixMode (All.Projection);
			GL.Ortho (0, frame.Width, 0, frame.Height, -1, 1);
			GL.MatrixMode (All.Modelview);
			GL.Enable (All.Texture2D);
			GL.EnableClientState (All.VertexArray);
			GL.Enable (All.Blend);
			GL.BlendFunc (All.SrcAlpha, All.One);
			GL.Enable (All.PointSpriteOes);
			GL.TexEnv (All.PointSpriteOes, All.CoordReplaceOes, (float) All.True);
			GL.PointSize (width / BrushScale);

			Erase ();

			PerformSelector (new Selector ("playback"), null, 0.2f);
		}
开发者ID:GSerjo,项目名称:monotouch-samples,代码行数:50,代码来源:PaintingView.cs


注:本文中的CGBitmapContext.DrawImage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。