예제 #1
0
        public static Image GetBlackAndWhiteImage(this Image image, float threshold = 0.5f)
        {
            var result = new Bitmap(image.Width, image.Height);

            var grayMatrix = new ColorMatrix(
                new[]
            {
                new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                new float[] { 0, 0, 0, 1, 0 },
                new float[] { 0, 0, 0, 0, 1 }
            });

            using (var g = Graphics.FromImage(result))
            {
                using (var ia = new ImageAttributes())
                {
                    ia.SetColorMatrix(grayMatrix);
                    ia.SetThreshold(threshold);
                    g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ia);
                }
            }

            return(result);
        }
예제 #2
0
            public static Bitmap Transform(Bitmap colorImage, float threshold = 0.8f)
            {
                var transformedImage = new Bitmap(colorImage.Width, colorImage.Height);

                var newGfx = Graphics.FromImage(transformedImage);

                var colorMatrix = new ColorMatrix(
                    new[]
                {
                    new[] { .3f, .3f, .3f, 0, 0 },
                    new[] { .59f, .59f, .59f, 0, 0 },
                    new[] { .11f, .11f, .11f, 0, 0 },
                    new float[] { 0, 0, 0, 1, 0 },
                    new float[] { 0, 0, 0, 0, 1 }
                });

                var imgAttr = new ImageAttributes();

                imgAttr.SetThreshold(threshold); // This threshold separates black from white

                imgAttr.SetColorMatrix(colorMatrix);

                newGfx.DrawImage(
                    colorImage,
                    new Rectangle(0, 0, colorImage.Width, colorImage.Height),
                    0,
                    0,
                    colorImage.Width,
                    colorImage.Height,
                    GraphicsUnit.Pixel,
                    imgAttr);

                newGfx.Dispose();
                return(transformedImage);
            }
예제 #3
0
        /// <summary>
        /// To the black white.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="threshold">The threshold.</param>
        /// <returns>Image.</returns>
        public static Image ToBlackWhite(this Image image, float threshold = 0.7f)
        {
            var outImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);

            using (Graphics gr = Graphics.FromImage(outImage))
            {
                var grayMatrix = new ColorMatrix(new float[][]
                {
                    new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                    new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                    new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                    new float[] { 0, 0, 0, 1, 0 },
                    new float[] { 0, 0, 0, 0, 1 }
                });

                var ia = new ImageAttributes();
                ia.SetColorMatrix(grayMatrix);
                ia.SetThreshold(threshold);

                var rc = new Rectangle(0, 0, image.Width, image.Height);
                gr.DrawImage(image, rc, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ia);

                return(outImage);
            }
        }
예제 #4
0
        public static Bitmap ModifyImageThreshold(Bitmap img)
        {
            float ThresholdValue = 0.35F;
            // float ThresholdValue = 0.55F;
            //float ThresholdValue = 0.35F;


            Bitmap          bm         = new Bitmap(img.Width, img.Height);
            ImageAttributes attributes = new ImageAttributes();

            attributes.SetThreshold(ThresholdValue);

            System.Drawing.Point[] points =
            {
                new System.Drawing.Point(0,                  0),
                new System.Drawing.Point(img.Width,          0),
                new System.Drawing.Point(0,         img.Height),
            };
            Rectangle rect = new Rectangle(0, 0, img.Width, img.Height);

            using (Graphics gr = Graphics.FromImage(bm))
            {
                gr.DrawImage(img, points, rect,
                             GraphicsUnit.Pixel, attributes);
            }
            bm.SetResolution(400, 400);
            return(bm);
        }
예제 #5
0
        public static Bitmap Grayscale(this Bitmap src, float level)
        {
            var cm = new ColorMatrix(
                new float[][]
            {
                new float[] { 0.299f, 0.299f, 0.299f, 0f, 0f },
                new float[] { 0.587f, 0.587f, 0.587f, 0f, 0f },
                new float[] { 0.114f, 0.114f, 0.114f, 0f, 0f },
                new float[] { 0, 0, 0, 1, 0 },
                new float[] { 0, 0, 0, 0, 1 },
            });

            var iAttr = new ImageAttributes();

            iAttr.SetColorMatrix(cm);
            iAttr.SetThreshold(level);

            var bmp = new Bitmap(src.Width, src.Height);

            using (var g = Graphics.FromImage(bmp))
            {
                g.DrawImage(src, new Rectangle(Point.Empty, src.Size), 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, iAttr);
            }

            return(bmp);
        }
예제 #6
0
        /// <summary>
        /// Adjust threshold to image.
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="value">a float value between 0 and 1</param>
        /// <returns></returns>
        public static Image AdjustThreshold(Image bmp, float value)
        {
            Image bmpNew = null;

            try
            {
                ImageAttributes ia = new ImageAttributes();
                ia.SetThreshold(value);

                bmpNew = new Bitmap(bmp.Width, bmp.Height);
                ((Bitmap)bmpNew).SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
                using (Graphics g = Graphics.FromImage(bmpNew))
                {
                    g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia);
                    ia.Dispose();
                }
            }
            catch
            {
                if (bmpNew != null)
                {
                    bmpNew.Dispose();
                    bmpNew = null;
                }
            }

            return(bmpNew);
        }
        public static Image ConvertToBlackWhite(Image image)
        {
            var grayMatrix = new[]
            {
                new[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                new[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                new[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                new float[] { 0, 0, 0, 1, 0 },
                new float[] { 0, 0, 0, 0, 1 }
            };

            var ia = new ImageAttributes();
            var rc = new Rectangle(0, 0, image.Width, image.Height);

            ia.SetColorMatrix(new ColorMatrix(grayMatrix));
            ia.SetThreshold(0.35f);

            var src    = new Bitmap(image);
            var target = new Bitmap(src.Size.Width, src.Size.Height);

            using (var g = Graphics.FromImage(target))
            {
                g.Clear(Color.White);
                g.SmoothingMode      = SmoothingMode.AntiAlias;
                g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.DrawImage(src, rc, 0, 0, target.Width, target.Height, GraphicsUnit.Pixel, ia);
            }

            return(target);
        }
예제 #8
0
        /// <summary>
        /// Returns a binarized image
        /// </summary>
        /// <param name="strength"></param>
        /// <returns></returns>
        public Bitmap Binarize(float strength)
        {
            if (strength < 0 || strength > 1)
            {
                throw new ArgumentOutOfRangeException("strength should be between 0 and 1");
            }

            var grayscale  = new Bitmap(source.Width, source.Height, source.PixelFormat);
            var attributes = new ImageAttributes();

            ColorMatrix grayscaleMatrix = new ColorMatrix(new float[][]
            {
                new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                new float[] { 0, 0, 0, 1, 0 },
                new float[] { 0, 0, 0, 0, 1 }
            });

            attributes.SetColorMatrix(grayscaleMatrix);
            attributes.SetThreshold(strength);

            using (var g = Graphics.FromImage(grayscale))
            {
                g.DrawImage(source, new Rectangle(0, 0, grayscale.Width, grayscale.Height), 0, 0, grayscale.Width, grayscale.Height, GraphicsUnit.Pixel, attributes);
            }

            return(grayscale);
        }
예제 #9
0
        private Bitmap ChangeColour(Bitmap original, ColorMatrix colorMatrix, bool setThresholdForBitonal)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            //create some image attributes
            ImageAttributes attributes = new ImageAttributes();

            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);

            // if the image is required is bitonal set a threshold
            if (setThresholdForBitonal)
            {
                attributes.SetThreshold(0.75f);
            }

            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
                        0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            //dispose the Graphics object
            g.Dispose();

            return(newBitmap);
        }
예제 #10
0
        public static Bitmap ConvertToBlackAndWhite(Bitmap original)
        {
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            Graphics graphics = Graphics.FromImage(newBitmap);

            ColorMatrix colorMatrix = new ColorMatrix(
                new[] {
                new float[] { 0.7f, 0.7f, 0.7f, 0, 0 },
                new float[] { 0.7f, 0.7f, 0.7f, 0, 0 },
                new float[] { 1.8f, 1.8f, 1.8f, 0, 0 },
                new float[] { 0, 0, 0, 1, 0 },
                new float[] { -1, -1, -1, 0, 1 }
            });

            ImageAttributes attributes = new ImageAttributes();

            attributes.SetColorMatrix(colorMatrix);
            attributes.SetThreshold(1);

            graphics.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            graphics.Dispose();

            return(newBitmap);
        }
예제 #11
0
        public static Bitmap Threshold(this Bitmap bitmap, float percent)
        {
            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetThreshold(percent);
            Bitmap   bmp = new Bitmap(bitmap);
            Graphics g   = Graphics.FromImage(bmp);

            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr);
            return(bmp);
        }
예제 #12
0
        private void trackBarThreshold_ValueChanged(object sender, EventArgs e)
        {
            labelThreshold.Text = "Threshold: " + trackBarThreshold.Value;

            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetThreshold((float)trackBarThreshold.Value);

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(pictureBoxOriginalImage.Image);
            Graphics g = System.Drawing.Graphics.FromImage(bmp);

            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0,
                        bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr);
            pictureBoxResultImage.Image = bmp;
        }
예제 #13
0
파일: Form1.cs 프로젝트: SamC0/origin
        public static Bitmap GetImageBW(Bitmap original)
        {
            Bitmap bmp = new Bitmap(original.Width, original.Height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                ImageAttributes ias = new ImageAttributes();
                ColorMatrix     m   = new ColorMatrix();
                m[0, 0] = m[0, 1] = m[0, 2] = 0.30f;
                m[1, 0] = m[1, 1] = m[1, 2] = 0.59f;
                m[2, 0] = m[2, 1] = m[2, 2] = 0.11f;
                ias.SetColorMatrix(m);  // 转化为灰度
                ias.SetThreshold(0.5f); // 单色门槛值 = 0.5
                g.DrawImage(original, new Rectangle(Point.Empty, original.Size), 0, 0, bmp.Width, bmp.Width, GraphicsUnit.Pixel, ias);
            }
            return(bmp.Clone(new Rectangle(Point.Empty, bmp.Size), PixelFormat.Format1bppIndexed));  // 输出单色图
        }
예제 #14
0
        public static Bitmap ToThreshold(this Bitmap bitmap, float threshold)
        {
            var result = new Bitmap(bitmap.Width, bitmap.Height);

            ImageAttributes attributes = new ImageAttributes();

            attributes.SetThreshold(threshold);

            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            using (var gr = Graphics.FromImage(result))
            {
                gr.DrawImage(bitmap, rect, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attributes);
            }

            return(result);
        }
예제 #15
0
        public static DirectBitmap ToThresholdGrayscale(this DirectBitmap bitmap, float threshold)
        {
            var result = new DirectBitmap(bitmap.Width, bitmap.Height);

            ImageAttributes attributes = new ImageAttributes();

            attributes.SetColorMatrix(new ColorMatrix(AppSettings.GrayscaleConversionMatrix));
            attributes.SetThreshold(threshold);

            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            using (var gr = Graphics.FromImage(result.Bitmap))
            {
                gr.DrawImage(bitmap.Bitmap, rect, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attributes);
            }

            return(result);
        }
예제 #16
0
        // </snippet6>


        // Snippet for: M:System.Drawing.Imaging.ImageAttributes.SetThreshold(System.Single)
        // <snippet7>
        private void SetThresholdExample(PaintEventArgs e)
        {
            // Open an Image file, and draw it to the screen.
            Image myImage = Image.FromFile("Camera.jpg");

            e.Graphics.DrawImage(myImage, 20, 20);

            // Create an ImageAttributes object, and set its color threshold.
            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetThreshold(0.7f);

            // Draw the image with the colors bifurcated.
            Rectangle rect = new Rectangle(300, 20, 200, 200);

            e.Graphics.DrawImage(myImage, rect, 0, 0, 200, 200,
                                 GraphicsUnit.Pixel, imageAttr);
        }
예제 #17
0
파일: Form1.cs 프로젝트: jenesepas/csharp
        private void menuItem2_Click(object sender, System.EventArgs e)
        {
            Graphics g = this.CreateGraphics();

            g.Clear(this.BackColor);
            Color           lClr    = Color.FromArgb(245, 0, 0);
            Color           uClr    = Color.FromArgb(255, 0, 0);
            ImageAttributes ImgAttr = new ImageAttributes();

            ImgAttr.SetThreshold(0.7f, ColorAdjustType.Default);
            Image curImage = Image.FromFile("dnWatcher.gif");

            g.DrawImage(curImage, 0, 0);
            Rectangle rect = new Rectangle(0, 0, 400, 400);

            g.DrawImage(curImage, rect, 0, 0, 400, 400,
                        GraphicsUnit.Pixel, ImgAttr);
            // Dispose
            g.Dispose();
        }
예제 #18
0
        public static Bitmap Threshold(Image img, float threshold, bool apply)
        {
            Bitmap bmp = new Bitmap(img.Width, img.Height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.White);                 //Threshold is the final transformation
                g.DrawImage(img, 0, 0);               //so clear any transparent color and apply threshold

                // Create an ImageAttributes object, and set its color threshold.
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetThreshold(threshold);

                if (apply)
                {
                    g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr);
                }
            }
            return(bmp);
        }
예제 #19
0
        public static Result <string> ExtractWordFromImage(OcrRequest request)
        {
            Image myImage   = WindowsHelper.CaptureWindowByName(request.ProcessName, new Rectangle(request.ImageXCoordinate, request.ImageYCoordinate, request.ImageWidth, request.ImageHeight));
            var   imageAttr = new ImageAttributes();

            imageAttr.SetThreshold(0.75f);
            var bm = new Bitmap(myImage.Width, myImage.Height);

            using (var gr = Graphics.FromImage(bm))
            {
                gr.DrawImage(myImage, new[] {
                    new Point(0, 0),
                    new Point(myImage.Width, 0),
                    new Point(0, myImage.Height),
                }, new Rectangle(0, 0, myImage.Width, myImage.Height),
                             GraphicsUnit.Pixel, imageAttr);
            }

            if (request.InvertColour)
            {
                for (var h = 0; h < bm.Height; h++)
                {
                    for (var w = 0; w < bm.Width; w++)
                    {
                        var color = bm.GetPixel(w, h);
                        bm.SetPixel(w, h, Color.FromArgb(255, (255 - color.R), (255 - color.G), (255 - color.B)));
                    }
                }
            }

            var fp = $"{Path.GetTempPath()}{Guid.NewGuid()}.jpg";

            bm.Save(fp);
            var word = new TesseractEngine($"{new System.IO.DirectoryInfo(Environment.CurrentDirectory).FullName}\\tessdata\\",
                                           "eng",
                                           EngineMode.Default).Process(Pix.LoadFromFile(@"D:\\tmp.jpg"))
                       .GetText()
                       .Replace("\n", " ");

            return(Result.SuccessIf <string>(!string.IsNullOrWhiteSpace(word), word, $"No words readable, image saved in {fp}"));
        }
예제 #20
0
        public static Bitmap Threshold(this Bitmap input, float value)
        {
            var result = new Bitmap(input.Width, input.Height);

            var attributes = new ImageAttributes();
            attributes.SetThreshold(value);

            var points = new System.Drawing.Point[]
            {
                new System.Drawing.Point(0, 0),
                new System.Drawing.Point(input.Width - 1, 0),
                new System.Drawing.Point(0, input.Height - 1),
            };

            var rect = new Rectangle(0, 0, input.Width, input.Height);

            using (var g = Graphics.FromImage(result))
                g.DrawImage(input, points, rect, GraphicsUnit.Pixel, attributes);

            return result;
        }
예제 #21
0
        public Bitmap ConvertBlackAndWhite(Bitmap source)
        {
            using (Graphics gr = Graphics.FromImage(source)) // SourceImage is a Bitmap object
            {
                var gray_matrix = new float[][] {
                    new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                    new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                    new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                    new float[] { 0, 0, 0, 1, 0 },
                    new float[] { 0, 0, 0, 0, 1 }
                };

                var ia = new ImageAttributes();
                ia.SetColorMatrix(new ColorMatrix(gray_matrix));
                ia.SetThreshold((float)0.8); // Change this threshold as needed
                var rc = new Rectangle(0, 0, source.Width, source.Height);
                gr.DrawImage(source, rc, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia);
            }

            return(source);
        }
예제 #22
0
        public static Bitmap MakeGrayscale(Bitmap original)
        {
            using (var gr = Graphics.FromImage(original))
            {
                var grayMatrix = new[]
                {
                    new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                    new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                    new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                    new float[] { 0, 0, 0, 1, 0 },
                    new float[] { 0, 0, 0, 0, 1 }
                };

                var ia = new ImageAttributes();
                ia.SetColorMatrix(new ColorMatrix(grayMatrix));
                ia.SetThreshold(0.8f); // Change this threshold as needed
                var rc = new Rectangle(0, 0, original.Width, original.Height);
                gr.DrawImage(original, rc, 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, ia);
            }

            return(original);
        }
예제 #23
0
        public void AdjustPicture()
        {
            if (original == null)
            {
                return;
            }

            if (pictureBox.Image != null)
            {
                pictureBox.Image.Dispose();
            }

            Bitmap bitmap = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);

            using (Graphics g = Graphics.FromImage(bitmap))
                using (ImageAttributes attributes = new ImageAttributes())
                {
                    RecalcCM();//Пересчитываем матрицу цвета
                    if (!isBW)
                    {
                        attributes.SetColorMatrix(cm);
                    }
                    else
                    {
                        attributes.SetColorMatrix(bwCM);
                        attributes.SetThreshold(threshold);
                    }

                    g.DrawImage(original, new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attributes);

                    if (isNoise)
                    {
                        BlendNoise(bitmap);
                    }

                    pictureBox.Image = bitmap;
                }
        }
예제 #24
0
        public Image ProcessImage(ImageFactory factory)
        {
            var    image  = factory.Image;
            Bitmap newBmp = null;

            try
            {
                // Make the result bitmap.
                newBmp = new Bitmap(image.Width, image.Height);

                // Make the ImageAttributes object and set the threshold.
                ImageAttributes attributes = new ImageAttributes();
                attributes.SetThreshold((float)DynamicParameter);

                // Draw the image onto the new bitmap
                // while applying the new ColorMatrix.
                Point[] points =
                {
                    new Point(0,                      0),
                    new Point(image.Width,            0),
                    new Point(0,           image.Height),
                };
                Rectangle rect =
                    new Rectangle(0, 0, image.Width, image.Height);
                using (Graphics gr = Graphics.FromImage(newBmp))
                {
                    gr.DrawImage(image, points, rect,
                                 GraphicsUnit.Pixel, attributes);
                }

                // Return the result.
            }
            catch (Exception ex)
            {
                newBmp?.Dispose();
                throw new ImageProcessingException("Error processing image with " + GetType().Name, ex);
            }
            return(newBmp);
        }
예제 #25
0
        public static Bitmap MakeBlackAndWhite(Image original, int threshold)
        {
            Bitmap _NewImage = new Bitmap(original);

            using (var _Graphics = Graphics.FromImage(_NewImage))
            {
                var _GrayMatrix = new[]
                {
                    new[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                    new[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                    new[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                    new[] { 0f, 0f, 0f, 1f, 0f },
                    new[] { 0f, 0f, 0f, 0f, 1f }
                };

                var _ImageAttributes = new ImageAttributes();
                _ImageAttributes.SetColorMatrix(new ColorMatrix(_GrayMatrix));
                _ImageAttributes.SetThreshold(threshold);

                var _Rectangle = new Rectangle(0, 0, original.Width, original.Height);
                _Graphics.DrawImage(original, _Rectangle, 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, _ImageAttributes);
            }
            return(_NewImage);
        }
예제 #26
0
        public static Bitmap makeGrayscale(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            using (Graphics g = Graphics.FromImage(newBitmap))
            {
                //create the grayscale ColorMatrix
                ColorMatrix colorMatrix = new ColorMatrix(
                    new float[][]
                {
                    new float[] { .3f, .3f, .3f, 0, 0 },
                    new float[] { .59f, .59f, .59f, 0, 0 },
                    new float[] { .11f, .11f, .11f, 0, 0 },
                    new float[] { 0, 0, 0, 1, 0 },
                    new float[] { 0, 0, 0, 0, 1 }
                });

                //create some image attributes
                using (ImageAttributes attributes = new ImageAttributes())
                {
                    //set the color matrix attribute
                    attributes.SetColorMatrix(colorMatrix);
                    attributes.SetThreshold(.7f);

                    //draw the original image on the new image
                    //using the grayscale color matrix
                    g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
                                0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
                    g.Dispose();
                    attributes.Dispose();
                }
            }
            return(newBitmap);
        }
예제 #27
0
    public static Image MakeMonochrome(this Image org, int threshold)
    {
        Bitmap mono_image = new Bitmap(org);

        var gray_matrix = new float[][] {
            new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
            new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
            new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
            new float[] { 0, 0, 0, 1, 0 },
            new float[] { 0, 0, 0, 0, 1 }
        };


        using (var attribute = new ImageAttributes())
            using (var gfx = Graphics.FromImage(mono_image))
            {
                attribute.SetColorMatrix(new ColorMatrix(gray_matrix));
                attribute.SetThreshold((float)threshold / 255);
                gfx.DrawImage(org, new Rectangle(0, 0, org.Width, org.Height),
                              0, 0, org.Width, org.Height, GraphicsUnit.Pixel, attribute);

                return(mono_image);
            }
    }