示例#1
0
        public static Bitmap HighlightBorder(Bitmap image, Point[] points, bool borderIsVertical)
        {
            int width  = image.Width;
            int height = image.Height;

            int scanLines      = borderIsVertical ? height : width;
            int scanLineLength = borderIsVertical ? width : height;

            byte[] outputBytes = BitmapBytesConverter.Get24BppBgrBytes(image);

            int index;

            for (int line = 0; line < scanLines; line++)
            {
                index = (points[line].Y * scanLineLength + points[line].X) * 3;
                outputBytes[index]     = (byte)(Byte.MaxValue - outputBytes[index]);
                outputBytes[index + 1] = (byte)(Byte.MaxValue - outputBytes[index + 1]);
                outputBytes[index + 2] = (byte)(Byte.MaxValue - outputBytes[index + 2]);
            }

            return(BitmapBytesConverter.Get24BppBitmap(outputBytes, width, height));
        }
示例#2
0
        private static Bitmap FilterColored(Bitmap input, Kernel kernel)
        {
            if (input == null)
            {
                return(input);
            }
            int width       = input.Width;
            int height      = input.Height;
            int kWidth      = kernel.Width;
            int kHeight     = kernel.Height;
            int kWidthHalf  = kWidth / 2;
            int kHeightHalf = kHeight / 2;

            int    outlinedWidth = width + kWidth - 1;
            Bitmap outlinedInput = new Bitmap(outlinedWidth, height + kHeight - 1);

            using (Graphics g = Graphics.FromImage(outlinedInput))
            {
                g.DrawImage(input, 1, 1);
            }
            byte[] inputRgbBytes = BitmapBytesConverter.Get24BppBgrBytes(outlinedInput);

            int borderBytes = ((kWidth - 1) * height + (kHeight - 1) * width + 4 * kWidthHalf * kHeightHalf) * 3;

            byte[] outputRgbBytes = new byte[inputRgbBytes.Length - borderBytes];

            double[] sum = { 0, 0, 0, 0 };
            double   kernelValue;
            int      index;
            int      x, y, i, j;

            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    sum[0] = sum[1] = sum[2] = sum[3] = 0;
                    for (i = 0; i < kWidth; i++)
                    {
                        for (j = 0; j < kHeight; j++)
                        {
                            kernelValue = kernel[i, j];
                            if (kernelValue != 0)
                            {
                                // обрабатываемый пиксель
                                index   = 3 * (outlinedWidth * (y + j) + x + i);
                                sum[0] += inputRgbBytes[index] * kernelValue;
                                sum[1] += inputRgbBytes[index + 1] * kernelValue;
                                sum[2] += inputRgbBytes[index + 2] * kernelValue;
                                sum[3] += kernelValue;
                            }
                        }
                    }
                    // сохраняемый пиксель
                    index = 3 * (width * y + x);
                    if (sum[3] <= 0)
                    {
                        sum[3] = 1;
                    }
                    for (byte t = 0; t < 3; t++)
                    {
                        sum[t] /= sum[3];
                        if (sum[t] < 0)
                        {
                            sum[t] = 0;
                        }
                        else if (sum[t] > 255)
                        {
                            sum[t] = 255;
                        }

                        outputRgbBytes[index + t] = (byte)sum[t];
                    }
                }
            }
            return(BitmapBytesConverter.Get24BppBitmap(outputRgbBytes, width, height));
        }