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


C# FastBitmap.SetColor方法代码示例

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


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

示例1: TickAlgorithm

        protected override Bitmap TickAlgorithm(Color?[,] current, Color?[,] next, ObjectPool<Bitmap> _pool, bool runParallel)
        {
            var bmp = _pool.GetObject();
            using (FastBitmap fastBmp = new FastBitmap(bmp))
            {
                // For every row
                Action<int> body = i =>
                {
                    // For every column
                    for (int j = 0; j < _height; j++)
                    {
                        int count = 0;
                        int r = 0, g = 0, b = 0;

                        // Count neighbors
                        for (int x = i - 1; x <= i + 1; x++)
                        {
                            for (int y = j - 1; y <= j + 1; y++)
                            {
                                if ((x == i && j == y) || x < 0 || x >= _width || y < 0 || y >= _height) continue;
                                Color? c = current[x, y];
                                if (c.HasValue)
                                {
                                    count++;
                                    r += c.Value.R;
                                    g += c.Value.G;
                                    b += c.Value.B;
                                }
                            }
                        }

                        // Heuristic for alive or dead based on neighbor count and current state

                        if (count < 2 || count >= 7) next[i, j] = null;
                        else if (current[i, j].HasValue && (count == 2 || count == 5 || count == 7)) next[i, j] = current[i, j];
                        else if (!current[i, j].HasValue && (count == 2 || count == 7)) next[i, j] = Color.FromArgb(r / count, g / count, b / count);
                        else next[i, j] = null;

                        // Render the cell
                        fastBmp.SetColor(i, j, current[i, j] ?? Color.White);
                    }
                };

                // Process the rows serially or in parallel based on the RunParallel property setting
                if (runParallel)
                {
                    Parallel.For(0, _width,
                        body);
                }
                else
                {
                for (int i = 0; i < _width; i++)
                    body(i);
                }
            }
            return bmp;
        }
开发者ID:kunalgujar,项目名称:GameOfLife,代码行数:57,代码来源:Rule27d257.cs

示例2: Colorize


//.........这里部分代码省略.........
                        bool pixelInSelectedRegion = false;

                        // First, if there are no paths, by definition it is in the selected
                        // region, since the whole image is then selected
                        if (paths == null || paths.Count == 0) pixelInSelectedRegion = true;
                        else
                        {
                            // For each path, first see if the pixel is within the bounding
                            // rectangle; if it's not, it's not in the selected region.
                            Point p = new Point(x, y);
                            for (int i = 0; i < pathsBounds.Length && !pixelInSelectedRegion; i++)
                            {
                                if (pathsBounds[i].Contains(p))
                                {
                                    // The pixel is within a bounding rectangle, so now
                                    // see if it's within the composition rectangles
                                    // approximating the region.
                                    foreach (RectangleF bound in compositions[i])
                                    {
                                        if (bound.Contains(x, y))
                                        {
                                            // If it is, it's in the region.
                                            pixelInSelectedRegion = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }

                        // Now that we know whether a pixel is in the region,
                        // we can figure out what to do with it.  If the pixel
                        // is not in the selected region, it needs to be converted
                        // to grayscale.
                        bool useGrayscale = true;
                        if (pixelInSelectedRegion)
                        {
                            // If it is in the selected region, get the color hue's distance 
                            // from each target hue.  If that distance is less than the user-selected
                            // hue variation limit, leave it in color.  If it's greater than the
                            // variation limit, but within the saturation window of the limit,
                            // desaturate it proportionally to the distance from the limit.
                            foreach (float selectedHue in selectedHues)
                            {
                                // A hue wheel is 360 degrees. If you pick two points on a wheel, there
                                // will be two distances between them, depending on which way you go around
                                // the wheel from one to the other (unless they're exactly opposite from
                                // each other on the wheel, the two distances will be different).  We always
                                // want to do our work based on the smaller of the two distances (e.g. a hue
                                // with the value 359 is very, very close to a hue with the value 1).  So,
                                // we take the absolute value of the difference between the two hues.  If that
                                // distance is 180 degrees, then both distances are the same, so it doesn't
                                // matter which we go with. If that difference is less than 180 degrees, 
                                // we know this must be the smaller of the two distances, since the sum of the 
                                // two distances must add up to 360.  If, however, it's larger than 180, it's the
                                // longer distance, so to get the shorter one, we have to subtract it from 360.
                                float distance = Math.Abs(pixelHue - selectedHue);
                                if (distance > 180) distance = 360 - distance;

                                if (distance <= epsilon)
                                {
                                    useGrayscale = false;
                                    break;
                                }
                                else if ((distance - epsilon) / saturationWindow < 1.0f)
                                {
                                    useGrayscale = false;
                                    c = ColorFromHsb(
                                        pixelHue,
                                        c.GetSaturation() * (1.0f - ((distance - epsilon) / maxSaturationWindow)),
                                        c.GetBrightness());
                                    break;
                                }
                            }
                        }

                        // Set the pixel color into the new image
                        if (useGrayscale) c = ToGrayscale(c);
                        fastColorizedImage.SetColor(x, y, c);
                    }

                    // Notify any listeners of our progress, if enough progress has been made
                    Interlocked.Add(ref pixelsProcessed, width);
                    OnProgressChanged((int)(100 * pixelsProcessed / (double)totalPixels));
                };

                // Copy over every single pixel, and possibly transform it in the process
                if (parallel)
                {
                    Parallel.For(0, height, processRow);
                }
                else
                {
                    for (int y = 0; y < height; y++) processRow(y);
                }
            }

            // We're done creating the image.  Return it.
            return colorizedImage;
        }
开发者ID:Farouq,项目名称:semclone,代码行数:101,代码来源:ImageManipulation.cs


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