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


C# BoundingBox.Clip方法代码示例

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


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

示例1: Airbrush

        /// <summary>
        /// Paints on a WriteableBitmap with a stylized airbrush
        /// </summary>
        /// <param name="bmp">The bitmap to modify</param>
        /// <param name="from">The starting point of the stroke</param>
        /// <param name="to">The end point of the stroke</param>
        /// <param name="color">The color of the stroke</param>
        /// <param name="size">The size of the stroke</param>
        public static unsafe void Airbrush(WriteableBitmap bmp, Point from, Point to, Color color, int size)
        {
            Random r = new Random();

            if (bmp == null) return;

            bmp.Lock();

            // Create a line segment representation
            MyLine line = new MyLine(from, to);

            // Get a bounding box for the painted area
            BoundingBox bitmapbounds = new BoundingBox();
            BoundingBox linebounds = new BoundingBox();

            bitmapbounds.AddPoint(0, 0, 0);
            bitmapbounds.AddPoint(bmp.PixelWidth - 1, bmp.PixelHeight - 1, 0);

            linebounds.AddPoint((int)from.X, (int)from.Y, size + AirbrushRadiu);
            linebounds.AddPoint((int)to.X, (int)to.Y, size + AirbrushRadiu);
            linebounds.Clip(bitmapbounds);

            UInt32* start = (UInt32*)bmp.BackBuffer.ToPointer();
            int stride = bmp.BackBufferStride / sizeof(UInt32);
            // Move from 'from' to 'to' along timestep intervals, with one dot painted per interval
            for (int i = 0; i < AirbrushDots; i++)
            {
                int x, y;
                line.Interpolate(i, AirbrushDots, out x, out y);

                int dist = r.Next() % size;
                double angle = r.NextDouble() * 2 * Math.PI;

                double dx = Math.Cos(angle) * dist;
                double dy = Math.Sqrt(dist * dist - dx * dx);
                if (angle > Math.PI) dy = -dy;

                int bx = x + (int)dx;
                int by = y + (int)dy;

                BoundingBox dotbounds = new BoundingBox();

                dotbounds.AddPoint(bx, by, AirbrushRadiu);
                dotbounds.Clip(bitmapbounds);

                for (int k = dotbounds.Top, row = 0; k < dotbounds.Bottom; k++, y++, row++)
                    for (int j = dotbounds.Left, col = 0; j < dotbounds.Right; j++, col++)
                        AlphaBlended(start + stride * k + j, Color.FromArgb(AirbrushBytes[row][col], color.R, color.G, color.B));
            }

            bmp.AddDirtyRect(new Int32Rect(linebounds.Left, linebounds.Top, linebounds.Width, linebounds.Height));
            bmp.Unlock();
        }
开发者ID:vasco-santos,项目名称:You_AirPaint,代码行数:61,代码来源:PaintTools.cs

示例2: Erase

        /// <summary>
        /// Erases paint on a WriteableBitmap
        /// </summary>
        /// <param name="bmp">The bitmap to modify</param>
        /// <param name="from">The starting point of the stroke</param>
        /// <param name="to">The end point of the stroke</param>
        /// <param name="size">The stroke size</param>
        public static unsafe void Erase(WriteableBitmap bmp, Point from, Point to, int size)
        {
            if (bmp == null) return;

            bmp.Lock();

            // Intermediate storage of the square of the size
            int area = size * size;

            // Create a line segment representation to compare distance to
            MyLine line = new MyLine(from, to);

            // Get a bounding box for the line segment
            BoundingBox bitmapbounds = new BoundingBox();
            BoundingBox linebounds = new BoundingBox();

            bitmapbounds.AddPoint(0, 0, 0);
            bitmapbounds.AddPoint(bmp.PixelWidth - 1, bmp.PixelHeight - 1, 0);

            linebounds.AddPoint((int)from.X, (int)from.Y, size);
            linebounds.AddPoint((int)to.X, (int)to.Y, size);
            linebounds.Clip(bitmapbounds);

            // Get a pointer to the back buffer (we use an int pointer here, since we can safely assume a 32-bit pixel format)
            Int32* start = (Int32*)bmp.BackBuffer.ToPointer();

            // Move the starting pixel to the x offset
            start += linebounds.Left;

            // Loop through the relevant portion of the image and figure out which pixels need to be erased
            for (int y = linebounds.Top; y < linebounds.Bottom; y++)
            {
                Int32* pixel = start + bmp.BackBufferStride / sizeof(Int32) * y;

                for (int x = linebounds.Left; x < linebounds.Right; x++)
                {
                    if (line.DistanceSquared(x, y) <= area)
                        *pixel = 0;

                    // Move to the next pixel
                    pixel++;
                }
            }

            bmp.AddDirtyRect(new Int32Rect(linebounds.Left, linebounds.Top, linebounds.Width, linebounds.Height));
            bmp.Unlock();
        }
开发者ID:vasco-santos,项目名称:You_AirPaint,代码行数:54,代码来源:PaintTools.cs

示例3: Brush

        /// <summary>
        /// Paints on a WriteableBitmap like a paintbrush
        /// </summary>
        /// <param name="bmp">The bitmap to modify</param>
        /// <param name="from">The starting point of the stroke</param>
        /// <param name="to">The end point of the stroke</param>
        /// <param name="previous">The point prior to the 'from' point, or null</param>
        /// <param name="color">The color of the brush</param>
        /// <param name="size">The stroke size</param>
        public static unsafe void Brush(WriteableBitmap bmp, Point from, Point to, Point? previous, Color color, int size)
        {
            if (bmp == null) return;

            bmp.Lock();

            // Intermediate storage of the square of the size
            int area = size * size;
            uint flatcolor = (uint)((int)color.A << 24) + (uint)((int)color.R << 16) + (uint)((int)color.G << 8) + color.B;

            // Create a line segment representation to compare distance to
            MyLine line = new MyLine(from, to);

            // Get a bounding box for the line segment
            BoundingBox bitmapbounds = new BoundingBox();
            BoundingBox linebounds = new BoundingBox();

            bitmapbounds.AddPoint(0, 0, 0);
            bitmapbounds.AddPoint(bmp.PixelWidth - 1, bmp.PixelHeight - 1, 0);

            linebounds.AddPoint((int)from.X, (int)from.Y, size);
            linebounds.AddPoint((int)to.X, (int)to.Y, size);
            linebounds.Clip(bitmapbounds);

            // Get a pointer to the back buffer (we use an int pointer here, since we can safely assume a 32-bit pixel format)
            UInt32* start = (UInt32*)bmp.BackBuffer.ToPointer();

            // Move the starting pixel to the x offset
            start += linebounds.Left;

            if (previous.HasValue)
            {
                MyLine previoussegment = new MyLine(previous.Value, from);

                // Loop through the relevant portion of the image and figure out which pixels need to be erased
                for (int y = linebounds.Top; y < linebounds.Bottom; y++)
                {
                    UInt32* pixel = start + bmp.BackBufferStride / sizeof(UInt32) * y;

                    for (int x = linebounds.Left; x < linebounds.Right; x++)
                    {
                        if (line.DistanceSquared(x, y) <= area && previoussegment.DistanceSquared(x, y) > area)
                        {
                            if (color.A == 255)
                                *pixel = flatcolor;
                            else
                                AlphaBlended(pixel, color);
                        }

                        // Move to the next pixel
                        pixel++;
                    }
                }
            }
            else
            {
                // Loop through the relevant portion of the image and figure out which pixels need to be erased
                for (int y = linebounds.Top; y < linebounds.Bottom; y++)
                {
                    UInt32* pixel = start + bmp.BackBufferStride / sizeof(UInt32) * y;

                    for (int x = linebounds.Left; x < linebounds.Right; x++)
                    {
                        if (line.DistanceSquared(x, y) <= area)
                        {
                            if (color.A == 255)
                                *pixel = flatcolor;
                            else
                                AlphaBlended(pixel, color);
                        }

                        // Move to the next pixel
                        pixel++;
                    }
                }
            }

            bmp.AddDirtyRect(new Int32Rect(linebounds.Left, linebounds.Top, linebounds.Width, linebounds.Height));
            bmp.Unlock();
        }
开发者ID:vasco-santos,项目名称:You_AirPaint,代码行数:89,代码来源:PaintTools.cs


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