Exemple #1
0
 public void StopPulsing()
 {
     if (timer != null)
     {
         iaDefault.SetGamma(1.0f, ColorAdjustType.Bitmap);
         timer.Stop();
     }
 }
Exemple #2
0
        public Bitmap AdjustBright(Bitmap originalImage)
        {
            //   https://stackoverflow.com/questions/15408607/adjust-brightness-contrast-and-gamma-of-an-image
            // string ImageSource = TemplatePath @"E:\GetGroup\Improvment\Images\mohabbest.jpg";

            Bitmap adjustedImage = new Bitmap(TemplatePath);
            float  brightness    = 1.0f; // no change in brightness
            float  contrast      = 2.0f; // twice the contrast
            float  gamma         = 1.0f; // no change in gamma

            float adjustedBrightness = brightness - 1.0f;

            // create matrix that will brighten and contrast the image
            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 1.0f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics g = Graphics.FromImage(adjustedImage);

            g.DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height)
                        , 0, 0, originalImage.Width, originalImage.Height,
                        GraphicsUnit.Pixel, imageAttributes);
            // adjustedImage.Save(@"E:\GetGroup\Improvment\Images\brighness.jpg");
            return(adjustedImage);
        }
Exemple #3
0
        public static Bitmap AdjustImage(Bitmap bmp, float p_brightness, float p_contrast, float p_gamma)
        {
            Bitmap originalImage;
            Bitmap adjustedImage;
            float  brightness = p_brightness; // 1.0f; // no change in brightness
            float  contrast   = p_contrast;   // 2.0f; // twice the contrast
            float  gamma      = p_gamma;      // 1.0f; // no change in gamma

            float adjustedBrightness = brightness - 1.0f;

            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 1.0f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.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, imageAttributes);

            return(bmp);
        }
Exemple #4
0
        /// <summary>
        /// The AdjustContrast function adjusts the brightness, contrast and gamma of the image and returns the newly adjusted image.
        /// </summary>
        /// <param name="bmp">Specifies the image to adjust.</param>
        /// <param name="fBrightness">Specifies the brightness to apply.</param>
        /// <param name="fContrast">Specifies the contrast to apply.</param>
        /// <param name="fGamma">Specifies the gamma to apply.</param>
        /// <returns>The updated image is returned.</returns>
        public static Bitmap AdjustContrast(Image bmp, float fBrightness = 1.0f, float fContrast = 1.0f, float fGamma = 1.0f)
        {
            float fAdjBrightNess = fBrightness - 1.0f;

            float[][] ptsArray =
            {
                new float[] { fContrast,                   0,              0,    0, 0 }, // scale red.
                new float[] {              0, fContrast,                   0,    0, 0 }, // scale green.
                new float[] {              0,              0, fContrast,         0, 0 }, // scale blue.
                new float[] {              0,              0,              0, 1.0f, 0 }, // don't scale alpha.
                new float[] { fAdjBrightNess, fAdjBrightNess, fAdjBrightNess,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(fGamma, ColorAdjustType.Bitmap);

            Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);

            using (Graphics g = Graphics.FromImage(bmpNew))
            {
                g.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttributes);
            }

            return(bmpNew);
        }
Exemple #5
0
        private Bitmap DarkenImage(Bitmap originalImage, float contrast)
        {
            Bitmap adjustedImage = new Bitmap(originalImage.Width, originalImage.Height);
            float  brightness    = 1.0f;

            float gamma = 1.0f; // no change in gamma

            float adjustedBrightness = brightness - 1.0f;

            // create matrix that will brighten and contrast the image
            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 1.0f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            using (Graphics g = Graphics.FromImage(adjustedImage))
            {
                g.DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height)
                            , 0, 0, originalImage.Width, originalImage.Height,
                            GraphicsUnit.Pixel, imageAttributes);
            }
            return(adjustedImage);
        }
        //Code from http://csharphelper.com/blog/2016/12/provide-gamma-correction-for-an-image-in-c/
        private Bitmap AdjustGamma(Image image)
        {
            // Set the ImageAttributes object's gamma value.
            ImageAttributes attributes = new ImageAttributes();

            attributes.SetGamma(.45f);

            // Draw the image onto the new bitmap
            // while applying the new gamma value.
            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);

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

            using (Graphics gr = Graphics.FromImage(bm))
            {
                gr.DrawImage(image, points, rect,
                             GraphicsUnit.Pixel, attributes);
            }

            // Return the result.
            return(bm);
        }
Exemple #7
0
        /// <summary>
        /// Adjusts the red, green and blue parts of the image
        ///
        /// Examples:
        /// Original image should be dark/black (main color RGB(51,51,51) and less)
        ///
        ///    blue (metro): 0,1,3,0.55
        ///    light grey: 1,1,1,0.35
        ///    white: 1,1,1,0.01
        ///    green: 0,2,0,0.75
        ///    red: 2,0,0,0.75
        /// </summary>
        /// <param name="img">Image</param>
        /// <param name="red">red part adjustment (>= 0.00, 1.0 = no changes)</param>
        /// <param name="green">red part adjustment (>= 0.00, 1.0 = no changes)</param>
        /// <param name="blue">blue part adjustment (>= 0.00, 1.0 = no changes)</param>
        /// <param name="gamma">Gamma adjustment (> 0.00, 1.0 = no changes)</param>
        /// <returns>the modified Image</returns>
        public static Image AdjustRGBGamma(this Image img, float red, float green, float blue, float gamma)
        {
            Bitmap bmp = new Bitmap(img.Width, img.Height);

            ImageAttributes imgAttributes = new ImageAttributes();

            ColorMatrix matrix = new ColorMatrix(
                new float[][] {
                new float[] { red, 0, 0, 0, 0 },
                new float[] { 0, green, 0, 0, 0 },
                new float[] { 0, 0, blue, 0, 0 },
                new float[] { 0, 0, 0, 1.0f, 0 },
                new float[] { 0, 0, 0, 0, 1 },
            });

            imgAttributes.ClearColorMatrix();
            imgAttributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imgAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height),
                            0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes);
            }

            return((Image)bmp);
        }
Exemple #8
0
        /// <summary>
        /// Gamma correction.
        /// </summary>
        /// <param name="bmp">Source image</param>
        /// <param name="value">Should be greater than 0.0. Typical values range between 1 and 2.2.</param>
        /// <returns></returns>
        public static Image AdjustGamma(Image bmp, float value)
        {
            Image bmpNew = null;

            try
            {
                ImageAttributes ia = new ImageAttributes();
                ia.SetGamma(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 void AdjustGammaTest()
        {
            const float gamma = 1.6f;

            using var bmp = Icons.Shield.ExtractBitmap(new Size(256, 256));
            new PerformanceTest {
                Iterations = 100, CpuAffinity = null
            }
            .AddCase(() =>
            {
                using var result = bmp.CloneBitmap();
                result.AdjustGamma(gamma);
            }, "BitmapExtensions.AdjustGamma")
            .AddCase(() =>
            {
                using var result = bmp.CloneBitmap();
                using (Graphics g = Graphics.FromImage(result))
                {
                    using (var attrs = new ImageAttributes())
                    {
                        attrs.SetGamma(gamma);
                        g.DrawImage(result, new Rectangle(Point.Empty, result.Size), 0, 0, result.Width, result.Height, GraphicsUnit.Pixel, attrs);
                    }
                }
            }, "Graphics.DrawImage(..., ImageAttributes)")
            .DoTest()
            .DumpResults(Console.Out);
        }
Exemple #10
0
        // Gamma
        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            label6.Text = trackBar4.Value.ToString();

            gamma = 0.04f * trackBar4.Value;

            Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);

            Graphics graphics = Graphics.FromImage(newBitmap);

            ImageAttributes attributes = new ImageAttributes();

            attributes.SetGamma(gamma);

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

            graphics.Dispose();

            attributes.Dispose();

            pictureBox1.Image = newBitmap;

            previousBitmap = new Bitmap(newBitmap);

            state.save(pictureBox1.Image, trackBar1.Value, trackBar2.Value, trackBar3.Value, trackBar4.Value);
            undoToolStripMenuItem.Enabled = true;
        }
Exemple #11
0
        private void SetNoOp_Click(object sender,
                                   System.EventArgs e)
        {
            // Create a Graphics object
            Graphics g = this.CreateGraphics();

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

            // Set ColorKey
            ImgAttr.SetColorKey(lClr, uClr,
                                ColorAdjustType.Default);
            // Set gamma
            ImgAttr.SetGamma(2.0f, ColorAdjustType.Default);
            // Set NoOp
            ImgAttr.SetNoOp(ColorAdjustType.Default);
            // Create an Image object
            Image curImage = Image.FromFile("dnWatcher.gif");
            // Draw Image
            Rectangle rect = new Rectangle(0, 0, 400, 400);

            g.DrawImage(curImage, rect, 0, 0, 400, 400,
                        GraphicsUnit.Pixel, ImgAttr);
            // Dispose
            g.Dispose();
        }
Exemple #12
0
        /// <summary>
        /// Получение копии рисунка с исправленной гаммой.
        /// </summary>
        public static Image ReGamma
        (
            Image image,
            Rectangle dstRect,
            float gamma
        )
        {
            Image result = new Bitmap(image.Width, image.Height);

            using (Graphics g = Graphics.FromImage(result))
                using (ImageAttributes attr = new ImageAttributes())
                {
                    attr.SetGamma(gamma, ColorAdjustType.Bitmap);
                    g.DrawImage
                    (
                        image,
                        dstRect,
                        0f,
                        0f,
                        image.Width,
                        image.Height,
                        GraphicsUnit.Pixel,
                        attr
                    );
                }

            return(result);
        }
Exemple #13
0
        void BrightenImage(string myFile)
        {
            using (var originalImage = (Bitmap)Image.FromFile(myFile)) {
                using (var adjustedImage = new Bitmap(originalImage.Width, originalImage.Height)) {
                    float brightness = -1.0f; // darker
                    float contrast   = 2.0f;  // twice the contrast
                    float gamma      = 1.0f;  // half the gamma

                    // create matrix that will brighten and contrast the image
                    float[][] ptsArray =
                    {
                        new float[] { contrast,            0,          0,    0, 0 }, // scale red
                        new float[] {          0, contrast,            0,    0, 0 }, // scale green
                        new float[] {          0,          0, contrast,      0, 0 }, // scale blue
                        new float[] {          0,          0,          0, 1.0f, 0 }, // don't scale alpha
                        new float[] { brightness, brightness, brightness,    0, 1 }
                    };

                    ImageAttributes imageAttributes = new ImageAttributes();
                    imageAttributes.ClearColorMatrix();
                    imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
                    Graphics g = Graphics.FromImage(adjustedImage);
                    g.DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height)
                                , 0, 0, originalImage.Width, originalImage.Height,
                                GraphicsUnit.Pixel, imageAttributes);
                    adjustedImage.Save(Path.Combine(Path.GetDirectoryName(myFile), Path.GetFileNameWithoutExtension(myFile) + "-adj.png"));
                }
            }
        }
        static void Adjust(float gamma, float contrast)
        {
            var destRect  = new Rectangle(0, 0, Img.Width, Img.Height);
            var destImage = new Bitmap(Img.Width, Img.Height);

            destImage.SetResolution(Img.HorizontalResolution, Img.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;

                using (var wrapMode = new ImageAttributes())
                {
                    float[][] ptsArray =
                    {
                        new[] { contrast,       0f,       0f, 0f, 0f }, // scale red
                        new[] {       0f, contrast,       0f, 0f, 0f }, // scale green
                        new[] {       0f,       0f, contrast, 0f, 0f }, // scale blue
                        new[] {       0f,       0f,       0f, 1f, 0f }, // don't scale alpha
                        new[] {       0f,       0f,       0f, 0f, 1f }
                    };

                    wrapMode.ClearColorMatrix();
                    wrapMode.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    wrapMode.SetGamma(gamma, ColorAdjustType.Bitmap);
                    graphics.DrawImage(Img, destRect, 0, 0, Img.Width, Img.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            Img = destImage;
        }
Exemple #15
0
        private static async Task <Bitmap> EnhanceImage(Bitmap originalImage)
        {
            Bitmap adjustedImage = new Bitmap(originalImage);
            float  brightness    = 1.0f; // no change in brightness
            float  contrast      = 3.5f; // twice the contrast
            float  gamma         = 1.0f; // no change in gamma

            float adjustedBrightness = brightness - 1.0f;

            // create matrix that will brighten and contrast the image
            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 1.0f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            var imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);

            Graphics g = Graphics.FromImage(adjustedImage);

            g.DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height)
                        , 0, 0, originalImage.Width, originalImage.Height,
                        GraphicsUnit.Pixel, imageAttributes);

            //originalImage.Save("C:\\TEMP\\ORG.PNG");
            //adjustedImage.Save("C:\\TEMP\\ADJ11.PNG");
            return(adjustedImage);
        }
Exemple #16
0
        private Bitmap AdjustImage(Bitmap originalImage, float brightness)
        {
            Bitmap adjustedImage      = new Bitmap(originalImage);
            float  constrast          = 1f;
            float  gamma              = 1f;
            float  adjustedBrightness = brightness - 1f;

            float[][] CS0 = new float[5][];
            float[]   CS1 = new float[5];
            CS1[0] = constrast;
            CS0[0] = CS1;
            float[] CS2 = new float[5];
            CS2[1] = constrast;
            CS0[1] = CS2;
            float[] CS3 = new float[5];
            CS3[2] = constrast;
            CS0[2] = CS3;
            float[] CS4 = new float[5];
            CS4[3] = 1f;
            CS0[3] = CS4;
            float[] CS5 = new float[5];
            CS5[0] = adjustedBrightness;
            CS5[1] = adjustedBrightness;
            CS5[2] = adjustedBrightness;
            CS5[4] = 1f;
            CS0[4] = CS5;
            float[][]       ptsArray        = CS0;
            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics.FromImage(adjustedImage).DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel, imageAttributes);
            return(adjustedImage);
        }
Exemple #17
0
        /// <summary>
        /// Applies matrix to the input bitmap
        /// </summary>
        /// <param name="bitmap">Bitmap, in standard color space</param>
        /// <param name="matrix">Matrix to use</param>
        /// <returns>Bitmap with matrix applied, in linear color space</returns>
        private static Bitmap ApplyLinearMatrix(Bitmap bitmap, float[][] matrix)
        {
            float[][] rgbMatrix =
            {
                new[] { matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],  0 },
                new[] { matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],  0 },
                new[] { matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],  0 },
                new[] { matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3],  0 },
                new[] {            0,            0,            0,            0, 1f }
            };
            var colorMatrix = new ColorMatrix(rgbMatrix);

            var targetBitmap = new Bitmap(bitmap);

            using (var targetGraphics = Graphics.FromImage(targetBitmap))
            {
                var imageAttributes = new ImageAttributes();
                imageAttributes.SetColorMatrix(colorMatrix);
                imageAttributes.SetGamma(1 / 2.2f);
                var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                targetGraphics.DrawImage(
                    bitmap,
                    rect,
                    0,
                    0,
                    bitmap.Width,
                    bitmap.Height,
                    GraphicsUnit.Pixel,
                    imageAttributes);
            }

            return(targetBitmap);
        }
Exemple #18
0
        //Adjust brightness contrast and gamma of an image
        private Bitmap imgBalance(Bitmap img, int brigh, int cont, int gam)
        {
            lblStatus.Text = "Balancing...";
            Refresh();
            ImageAttributes imageAttributes;
            float           brightness         = (brigh / 100.0f) + 1.0f;
            float           contrast           = (cont / 100.0f) + 1.0f;
            float           gamma              = 1 / (gam / 100.0f);
            float           adjustedBrightness = brightness - 1.0f;
            Bitmap          output;

            // create matrix that will brighten and contrast the image
            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 1.0f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            output          = new Bitmap(img);
            imageAttributes = new ImageAttributes();
            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics g = Graphics.FromImage(output);

            g.DrawImage(output, new Rectangle(0, 0, output.Width, output.Height)
                        , 0, 0, output.Width, output.Height,
                        GraphicsUnit.Pixel, imageAttributes);
            lblStatus.Text = "Done";
            Refresh();
            return(output);
        }
Exemple #19
0
        Bitmap Change_brightnessbutton(Bitmap b1)
        {
            Bitmap originalImage = b1;
            Bitmap adjustedImage = b1;
            float  brightness    = 0.8f; // no change in brightness
            float  contrast      = 2.0f; // twice the contrast
            float  gamma         = 1.0f; // no change in gamma

            float adjustedBrightness = brightness - 1.0f;

            // create matrix that will brighten and contrast the image
            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 0.3f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics g = Graphics.FromImage(adjustedImage);

            g.DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height)
                        , 0, 0, originalImage.Width, originalImage.Height,
                        GraphicsUnit.Pixel, imageAttributes);

            return(adjustedImage);
        }
Exemple #20
0
        // </snippet4>


        // Snippet for: M:System.Drawing.Imaging.ImageAttributes.SetNoOp
        // <snippet5>
        private void SetNoOpExample(PaintEventArgs e)
        {
            // Create an Image object from the file Camera.jpg.
            Image myImage = Image.FromFile("Camera.jpg");

            // Create an ImageAttributes object, and set the gamma to 0.25.
            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetGamma(0.25f);

            // Draw the image with gamma set to 0.25.
            Rectangle rect1 = new Rectangle(20, 20, 200, 200);

            e.Graphics.DrawImage(myImage, rect1, 0, 0, 200, 200,
                                 GraphicsUnit.Pixel, imageAttr);

            // Call the ImageAttributes NoOp method.
            imageAttr.SetNoOp();

            // Draw the image after NoOp is set, so the default gamma value
            // of 1.0 will be used.
            Rectangle rect2 = new Rectangle(250, 20, 200, 200);

            e.Graphics.DrawImage(myImage, rect2, 0, 0, 200, 200,
                                 GraphicsUnit.Pixel, imageAttr);
        }
Exemple #21
0
    public static Image Adjust(this Image org, float brightness = 1.0f, float contrast = 1.0f, float gamma = 1.0f)
    {
        Bitmap adjusted_image = new Bitmap(org);

        brightness -= 1.0f;

        // create matrix that will brighten and contrast the image
        float[][] ptsArray =
        {
            new float[] { contrast,            0,          0,    0, 0 }, // scale red
            new float[] {          0, contrast,            0,    0, 0 }, // scale green
            new float[] {          0,          0, contrast,      0, 0 }, // scale blue
            new float[] {          0,          0,          0, 1.0f, 0 }, // don't scale alpha
            new float[] { brightness, brightness, brightness,    0, 1 }
        };

        using (var gfx = Graphics.FromImage(adjusted_image))
            using (var attributes = new ImageAttributes())
            {
                attributes.ClearColorMatrix();
                attributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                attributes.SetGamma(gamma, ColorAdjustType.Bitmap);
                gfx.DrawImage(org, new Rectangle(0, 0, org.Width, org.Height),
                              0, 0, org.Width, org.Height, GraphicsUnit.Pixel, attributes);
            }

        return(adjusted_image);
    }
Exemple #22
0
        public void CopyBitmap(Image image)
        {
            var img = finalImage;

            // convert to rgb
            for (int i = 0; i < finalImage.GetLength(0); i++)
            {
                for (int j = 0; j < finalImage.GetLength(1); j++)
                {
                    var rgb = finalImage[i, j]?.ToRGB();

                    if (rgb == null)
                    {
                        finalRgbImage.SetPixel(i, pixelHeight - 1 - j, Color.Black);
                    }
                    else
                    {
                        //Console.WriteLine(rgb[0]);
                        rgb[0] = rgb[0].Clamp(0, 1.0039);
                        //Console.WriteLine(rgb[0]);
                        rgb[1] = rgb[1].Clamp(0, 1.0039);
                        rgb[2] = rgb[2].Clamp(0, 1.0039);
                        Color c = Color.FromArgb((int)Math.Floor(255 * rgb[0]), (int)Math.Floor(255 * rgb[1]), (int)Math.Floor(255 * rgb[2]));
                        finalRgbImage.SetPixel(i, pixelHeight - 1 - j, c);
                    }
                }
            }
            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.SetGamma(2.4f);
            using (Graphics grD = Graphics.FromImage(image))
            {
                grD.DrawImage(finalRgbImage, new Rectangle(Point.Empty, image.Size), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            }
        }
Exemple #23
0
        public Bitmap gammaContrast(float contrast, float gamma, float brightness, Bitmap img)
        {
            Bitmap img2;

            img2 = img.Clone() as Bitmap;
            Bitmap adjustedImage = img2.Clone() as Bitmap;



            // create matrix that will brighten and contrast the image
            float[][] colorMatrix =
            {
                new float[] { contrast,                          0,                 0,    0, 0 }, // scale red
                new float[] {                 0, contrast,                          0,    0, 0 }, // scale green
                new float[] {                 0,                 0, contrast,             0, 0 }, // scale blue
                new float[] {                 0,                 0,                 0, 1.0f, 0 }, // don't scale alpha
                new float[] { brightness - 1.0f, brightness - 1.0f, brightness - 1.0f,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(colorMatrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics g = Graphics.FromImage(adjustedImage);

            g.DrawImage(img2, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height)
                        , 0, 0, img2.Width, img2.Height,
                        GraphicsUnit.Pixel, imageAttributes);
            return(adjustedImage.Clone() as Bitmap);
        }
Exemple #24
0
        public static Bitmap AdjustBitmap(Bitmap originalImage, float brightness, float contrast, float gamma)
        {
            var result = new Bitmap(originalImage.Width, originalImage.Height);

            /*float brightness = 1.0f; // no change in brightness
             * float contrast = 2.0f; // twice the contrast
             * float gamma = 1.0f; // no change in gamma*/

            float adjustedBrightness = brightness - 1.0f;

            // create matrix that will brighten and contrast the image
            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 1.0f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            var graphics = Graphics.FromImage(result);

            graphics.DrawImage(originalImage, new Rectangle(0, 0, result.Width, result.Height)
                               , 0, 0, originalImage.Width, originalImage.Height,
                               GraphicsUnit.Pixel, imageAttributes);

            return(result);
        }
Exemple #25
0
        private Image decreaseBlur()
        {
            float gamma = m_GammaAmount * 0.04f;

            Bitmap newBitmap = new Bitmap(m_Image);

            Bitmap bitMapWanted = new Bitmap(newBitmap.Width, newBitmap.Height);

            Graphics graphics = Graphics.FromImage(bitMapWanted);

            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetGamma(gamma);

            graphics.DrawImage(
                newBitmap,
                new Rectangle(0, 0, newBitmap.Width, newBitmap.Height),
                0,
                0,
                newBitmap.Width,
                newBitmap.Height,
                GraphicsUnit.Pixel,
                imageAttr);

            graphics.Dispose();
            imageAttr.Dispose();

            return(bitMapWanted);
        }
Exemple #26
0
        /// <summary>
        /// Change the intensity of the image
        /// </summary>
        /// <param name="image">The dot image</param>
        /// <param name="weight">The weight to apply</param>
        /// <returns>The weighted image</returns>
        private static Bitmap ApplyHeatValueToImage(Bitmap image, float weight)
        {
            var tempImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);

            using (var g = Graphics.FromImage(tempImage))
            {
                //I want to make the color more intense (White/bright)
                if (weight < 0.02f)
                {
                    weight = 0.02f;
                }
                weight *= 5f;
                if (weight > 5f)
                {
                    weight = 5f;
                }

                // Create ImageAttributes
                var ia = new ImageAttributes();

                //Gamma values range from 0.1 to 5.0 (normally 0.1 to 2.2), with 0.1 being the brightest and 5.0 the darkest.
                //Convert the 100% to a range of 0.1-5 by multiplying it by 5
                ia.SetGamma(weight, ColorAdjustType.Bitmap);

                // Draw Image with the attributes
                g.DrawImage(image,
                            new Rectangle(0, 0, image.Width, image.Height),
                            0, 0, image.Width, image.Height,
                            GraphicsUnit.Pixel, ia);
            }
            //New dot with a different intensity
            return(tempImage);
        }
Exemple #27
0
        //Make background image white for level transition animation
        //Зміна кольору стін лабіринту на білий для анімації переходу на наступний рівень
        public Bitmap AdjustColor()
        {
            Bitmap changedMap = new Bitmap(map.Width, map.Height); //adjustedImage;
            float  brightness = 2.0f;                              // no change in brightness
            float  contrast   = 0.0f;                              // twice the contrast
            float  gamma      = 1.0f;                              // no change in gamma

            float adjustedBrightness = brightness - 1.0f;

            // create matrix that will brighten and contrast the image
            float[][] ptsArray =
            {
                new float[] { contrast,                            0,                  0,    0, 0 }, // scale red
                new float[] {                  0, contrast,                            0,    0, 0 }, // scale green
                new float[] {                  0,                  0, contrast,              0, 0 }, // scale blue
                new float[] {                  0,                  0,                  0, 1.0f, 0 }, // don't scale alpha
                new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics g = Graphics.FromImage(changedMap);

            g.DrawImage(map, new Rectangle(0, 0, changedMap.Width, changedMap.Height)
                        , 0, 0, map.Width, map.Height,
                        GraphicsUnit.Pixel, imageAttributes);
            return(changedMap);
        }
Exemple #28
0
        public virtual Image GetThumbnailImage(Image source, int width, int height, ResizeMode mode, Color paddingColor)
        {
            Rectangle canvasSize      = CalculateCanvasRectangle(source, width, height, mode);
            Bitmap    canvas          = new Bitmap(canvasSize.Width, canvasSize.Height, PixelFormat.Format24bppRgb);
            Rectangle sourceRectangle = CalculateCropZoomRectangle(source.Width, source.Height, canvas.Width,
                                                                   canvas.Height);

            using (Graphics graphics = Graphics.FromImage(canvas))
            {
                graphics.SmoothingMode      = SmoothingMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighSpeed;
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                using (SolidBrush brush = new SolidBrush(paddingColor))
                {
                    graphics.FillRectangle(brush, 0, 0, canvas.Width, canvas.Height);

                    Point upperLeft  = new Point(0, 0);
                    Point upperRight = new Point(canvas.Width, 0);
                    Point lowerLeft  = new Point(0, canvas.Height);

                    Point[]         destinationPoints = new Point[] { upperLeft, upperRight, lowerLeft };
                    ImageAttributes ia = new ImageAttributes();
                    ia.SetGamma(1.5f);
                    graphics.DrawImage(source, destinationPoints, sourceRectangle, GraphicsUnit.Pixel, ia);
                }
            }
            return(canvas);
        }
        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            try
            {
                lblGamma.Text = trackBar4.Value.ToString();

                gamma = 0.04f * trackBar4.Value;

                Bitmap bm = new Bitmap(newBitmap.Width, newBitmap.Height);

                Graphics        gMustafaAlparslanPamuk = Graphics.FromImage(bm);
                ImageAttributes iaPamuk = new ImageAttributes();

                iaPamuk.SetGamma(gamma);

                gMustafaAlparslanPamuk.DrawImage(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), 0, 0, newBitmap.Width, newBitmap.Height, GraphicsUnit.Pixel, iaPamuk);
                gMustafaAlparslanPamuk.Dispose();
                iaPamuk.Dispose();
                pictureBox1.Image = bm;
            }
            catch (Exception)
            {
                MessageBox.Show("Gamma value can not be 0!", "Mustafa Alparslan Pamuk | Image Converter Program", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #30
0
        public static Bitmap Adjust(Bitmap originalImage, float brightness, float contrast, float gamma)
        {
            Bitmap adjustedImage      = new Bitmap(originalImage.Width, originalImage.Height);
            float  adjustedBrightness = brightness - 1.0f;

            float[][] ptsArray =
            {
                new[] { contrast,                            0,                  0,    0, 0 },
                new[] {                  0, contrast,                            0,    0, 0 },
                new[] {                  0,                  0, contrast,              0, 0 },
                new[] {                  0,                  0,                  0, 1.0f, 0 },
                new[] { adjustedBrightness, adjustedBrightness, adjustedBrightness,    0, 1 }
            };

            var imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics g = Graphics.FromImage(adjustedImage);

            g.DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height)
                        , 0, 0, originalImage.Width, originalImage.Height,
                        GraphicsUnit.Pixel, imageAttributes);
            return(adjustedImage);
        }
Exemple #31
0
	public static void Main(string[] args)
	{	
		Graphics.DrawImageAbort imageCallback;
		Bitmap outbmp = new Bitmap (600, 600);				
		Bitmap bmp = new Bitmap("../../Test/System.Drawing/bitmaps/almogaver32bits.bmp");
		Graphics dc = Graphics.FromImage (outbmp);        
		SolidBrush br = new SolidBrush(Color.White);
		Bitmap img = bmp.Clone (new Rectangle (0,0, 60,60) , PixelFormat.Format32bppArgb);									
		
		ImageAttributes imageAttr = new ImageAttributes();
		
		Bitmap	bmpred = new Bitmap (100,100, PixelFormat.Format32bppArgb);		
		Graphics gr = Graphics.FromImage (bmpred);		
		
		/* Sample drawing*/
		Pen cyan = new Pen(Color.Cyan, 0);
		Pen green = new Pen(Color.Green, 0);
		Pen pink = new Pen(Color.Pink, 0);			
		Pen blue = new Pen(Color.Blue, 0);			
		gr.DrawLine(cyan, 10.0F, 10.0F, 90.0F, 90.0F);
		gr.DrawLine(pink, 10.0F, 30.0F, 90.0F, 30.0F);
		gr.DrawLine(green, 10.0F, 50.0F, 90.0F, 50.0F);
		gr.DrawRectangle (blue, 10.0F, 10.0F, 80.0F, 80.0F);				
		
		/* Draw image without any imageattributes*/		
		dc.DrawImage (bmpred, 0,0);				
		dc.DrawString ("Sample drawing", new Font ("Arial", 8), br,  10, 100);				
		
		/* Remmaping colours */
		ColorMap[] clr = new ColorMap[1];	
		clr[0] = new ColorMap(); 
		clr[0].OldColor = Color.Blue;
		clr[0].NewColor = Color.Yellow;	
		
		imageAttr.SetRemapTable (clr, ColorAdjustType.Bitmap);					
		dc.DrawImage (bmpred, new Rectangle (100, 0, 100,100), 0,0, 100,100, GraphicsUnit.Pixel, imageAttr);			
		dc.DrawString ("Remapping colors", new Font ("Arial", 8), br,  110, 100);				
		
		/* Gamma correction on*/
		imageAttr = new ImageAttributes();
		imageAttr.SetGamma (2);		
		dc.DrawImage (bmpred, new Rectangle (200, 0, 100,100), 0,0, 
			100,100, GraphicsUnit.Pixel, imageAttr);
			
		dc.DrawString ("Gamma corrected", new Font ("Arial", 8), br,  210, 100);				
		
		/* WrapMode: TitleX */
		imageAttr = new ImageAttributes();	
		imageAttr.SetWrapMode (WrapMode.TileFlipX);	
		
		dc.DrawImage (bmpred, new Rectangle (0, 120, 200, 200), 0,0, 
			200, 200, GraphicsUnit.Pixel, imageAttr);					
			
		dc.DrawString ("WrapMode.TileFlipX", new Font ("Arial", 8), br,  10, 320);								
			
		/* WrapMode: TitleY */		
		imageAttr.SetWrapMode (WrapMode.TileFlipY);	
		
		dc.DrawImage (bmpred, new Rectangle (200, 120, 200, 200), 0,0, 
			200, 200, GraphicsUnit.Pixel, imageAttr);				
			
		dc.DrawString ("WrapMode.TileFlipY", new Font ("Arial", 8), br,  210, 320);											
			
		/* WrapMode: TitleXY */		
		imageAttr.SetWrapMode (WrapMode.TileFlipXY);	
		
		dc.DrawImage (bmpred, new Rectangle (400, 120, 200, 200), 0,0, 
			200, 200, GraphicsUnit.Pixel, imageAttr);				
			
		dc.DrawString ("WrapMode.TileFlipXY", new Font ("Arial", 8), br,  410, 320);														
		
		outbmp.Save("imageattributes.bmp", ImageFormat.Bmp);				
		
	}