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;
        }
示例#2
0
        private static void HeavyImageProcessing(FileInfo file)
        {
            Bitmap originalImage = new Bitmap(file.FullName);
            Bitmap adjustedImage = new Bitmap(originalImage);
            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(file.Directory + @"\out\" + file.Name);
        }
示例#3
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);
        }
示例#4
0
        static Bitmap[] GetRgbChannels(Image source)
        {
            Bitmap[]        result          = new Bitmap[3];
            ImageAttributes imageAttributes = new ImageAttributes();

            ColorMatrix[] matrices = new ColorMatrix[3];

            for (int i = 0; i < matrices.Length; i++)
            {
                float[][] elements =
                {
                    new float[] { i == 0 ? 1 : 0,              0,              0, 0, 0 },
                    new float[] {              0, i == 1 ? 1 : 0,              0, 0, 0 },
                    new float[] {              0,              0, i == 2 ? 1 : 0, 0, 0 },
                    new float[] {              0,              0,              0, 1, 0 },
                    new float[] {              0,              0,              0, 0, 0 }
                };
                matrices[i] = new ColorMatrix(elements);
            }

            int w = source.Width, h = source.Height;

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = new Bitmap(source);
                imageAttributes.ClearColorMatrix();
                imageAttributes.SetColorMatrix(matrices[i], ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                using (Graphics g = Graphics.FromImage(result[i]))
                {
                    g.DrawImage(result[i], new Rectangle(0, 0, w, h), 0, 0, w, h, GraphicsUnit.Pixel, imageAttributes);
                }
            }
            return(result);
        }
    public static Image smethod_0(this Image image_0, float float_0, float float_1, float float_2, float float_3)
    {
        Bitmap          image     = new Bitmap(image_0.Width, image_0.Height);
        ImageAttributes imageAttr = new ImageAttributes();

        float[][] newColorMatrix = new float[5][];
        float[]   singleArray1   = new float[5];
        singleArray1[0]   = float_0;
        newColorMatrix[0] = singleArray1;
        float[] singleArray2 = new float[5];
        singleArray2[1]   = float_1;
        newColorMatrix[1] = singleArray2;
        float[] singleArray3 = new float[5];
        singleArray3[2]   = float_2;
        newColorMatrix[2] = singleArray3;
        float[] singleArray4 = new float[5];
        singleArray4[3]   = 1f;
        newColorMatrix[3] = singleArray4;
        float[] singleArray5 = new float[5];
        singleArray5[4]   = 1f;
        newColorMatrix[4] = singleArray5;
        ColorMatrix matrix = new ColorMatrix(newColorMatrix);

        imageAttr.ClearColorMatrix();
        imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        imageAttr.SetGamma(float_3, ColorAdjustType.Bitmap);
        using (Graphics graphics = Graphics.FromImage(image))
        {
            graphics.DrawImage(image_0, new Rectangle(0, 0, image_0.Width, image_0.Height), 0, 0, image_0.Width, image_0.Height, GraphicsUnit.Pixel, imageAttr);
        }
        return(image);
    }
示例#6
0
        public static Bitmap Brighten(this Image image, float brightness)
        {
            float contrast = 1.0f; // no change in 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[] { 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);

            Bitmap brightenedBitmap = new Bitmap(image.Width, image.Height);

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


            return(brightenedBitmap);
        }
示例#7
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"));
                }
            }
        }
示例#8
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);
        }
示例#9
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);
        }
示例#10
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);
        }
示例#11
0
        private void frmImage_Shown(object sender, EventArgs e)
        {
            Bitmap originalImage = (Bitmap)Image.FromFile("C:\\Customer\\Stannah\\PhotoGrammetry\\Photos\\survey 1 05-12-19\\survey 1\\Corners-Survey2.png");
            Bitmap adjustedImage = new Bitmap(originalImage.Width, originalImage.Height);
            float  brightness    = 0.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);
            pictureBox1.Image = adjustedImage;

            adjustedImage.Save("C:\\Customer\\Stannah\\PhotoGrammetry\\Photos\\survey 1 05-12-19\\survey 1\\Corners-Survey2a.png", ImageFormat.Png);
        }
示例#12
0
        static Bitmap[] GetRgbChannels(Image source)
        {
            Bitmap[]        result          = new Bitmap[3]; // объект для обработки изображения
            ImageAttributes imageAttributes = new ImageAttributes();

            ColorMatrix[] matrices = new ColorMatrix[3];
            for (int i = 0; i < matrices.Length; i++)
            {
                float[][] elements =
                {
                    new float[] { i == 0 ? 1 : 0,              0,              0, 0, 0 },
                    new float[] {              0, i == 1 ? 1 : 0,              0, 0, 0 },
                    new float[] {              0,              0, i == 2 ? 1 : 0, 0, 0 },
                    new float[] {              0,              0,              0, 1, 0 },
                    new float[] {              0,              0,              0, 0, 0 }
                };
                matrices[i] = new ColorMatrix(elements); // цветовая матрица
            }
            int w = source.Width, h = source.Height;     // ширина высота изображения

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = new Bitmap(source);                                                               //выбираем i картинку
                imageAttributes.ClearColorMatrix();                                                           //очищаем матрицу
                imageAttributes.SetColorMatrix(matrices[i], ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //задаём настройки цвета матрицы
                using (Graphics g = Graphics.FromImage(result[i]))
                {
                    g.DrawImage(result[i], new Rectangle(0, 0, w, h), 0, 0, w, h, GraphicsUnit.Pixel, imageAttributes);//перерисовка изображения
                }
            }
            return(result);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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);
        }
示例#16
0
        public void SetModulateColor(sd.Color color)
        {
            //white is really no color at all
            if (color.ToArgb() == sd.Color.White.ToArgb())
            {
                CurrentImageAttributes.ClearColorMatrix(ColorAdjustType.Bitmap);
                return;
            }

            float r = color.R / 255.0f;
            float g = color.G / 255.0f;
            float b = color.B / 255.0f;
            float a = color.A / 255.0f;

            float[][] colorMatrixElements =
            {
                new float[] { r, 0, 0, 0, 0 },
                new float[] { 0, g, 0, 0, 0 },
                new float[] { 0, 0, b, 0, 0 },
                new float[] { 0, 0, 0, a, 0 },
                new float[] { 0, 0, 0, 0, 1 }
            };

            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            CurrentImageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        }
示例#17
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);
        }
示例#18
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);
        }
示例#19
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);
        }
示例#20
0
        public Form1()
        {
            InitializeComponent();

            // ReSharper disable once VirtualMemberCallInConstructor
            MinimumSize = new Size(100, 100);
            // ReSharper disable once VirtualMemberCallInConstructor
            DoubleBuffered = true;

            const float brightness = 0.05f, contrast = 3.0f, gamma = 1.1f;

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

            _postProcess = new ImageAttributes();
            _postProcess.ClearColorMatrix();
            _postProcess.SetColorMatrix(new ColorMatrix(colorMatrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            _postProcess.SetGamma(gamma, ColorAdjustType.Bitmap);
        }
示例#21
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);
        }
示例#22
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);
    }
示例#23
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);
        }
示例#24
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);
        }
示例#25
0
        public static void ApplyColorMatrix(Bitmap source, Rectangle sourceRect, Bitmap dest, Rectangle destRect, ColorMatrix colorMatrix)
        {
            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.ClearColorMatrix();
            imageAttributes.SetColorMatrix(colorMatrix);
            ApplyImageAttributes(source, sourceRect, dest, destRect, imageAttributes);
        }
 /// <summary>
 ///     Apply a color matrix by copying from the source to the destination
 /// </summary>
 /// <param name="source">Image to copy from</param>
 /// <param name="sourceRect">NativeRect to copy from</param>
 /// <param name="destRect">NativeRect to copy to</param>
 /// <param name="dest">Image to copy to</param>
 /// <param name="colorMatrix">ColorMatrix to apply</param>
 public static void ApplyColorMatrix(this Bitmap source, NativeRect sourceRect, Bitmap dest, NativeRect destRect, ColorMatrix colorMatrix)
 {
     using (var imageAttributes = new ImageAttributes())
     {
         imageAttributes.ClearColorMatrix();
         imageAttributes.SetColorMatrix(colorMatrix);
         source.ApplyImageAttributes(sourceRect, dest, destRect, imageAttributes);
     }
 }
示例#27
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (gfx == null)
            {
                ReallocateGraphics();
            }

            switch (_mode)
            {
            case DisplayMode.BlackScreen:
                gfx.Graphics.FillRectangle(Brushes.Black, 0, 0, Width, Height);
                break;

            case DisplayMode.TV:
                RefreshBitmap();

                gfx.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

                float brightness = 1.1f;
                float contrast   = 1.0f;
                float gamma      = 1.0f;

                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);

                Rectangle rect = new Rectangle(0, 0, Width, Height);

                gfx.Graphics.DrawImage(_screenBitmap, rect,
                                       0, 0, _screenBitmap.Width, _screenBitmap.Height,
                                       GraphicsUnit.Pixel, imageAttributes);
                break;

            case DisplayMode.Native:

                // TODO

                break;
            }

            gfx.Render(e.Graphics);
        }
示例#28
0
 public static void Apply(this ColorMatrix matrix, Bitmap src, Bitmap dest, Rectangle destRect)
 {
     using (Graphics g = Graphics.FromImage(dest))
         using (ImageAttributes ia = new ImageAttributes())
         {
             ia.ClearColorMatrix();
             ia.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
             g.SetHighQuality();
             g.DrawImage(src, destRect, 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, ia);
         }
 }
示例#29
0
        private void RenderFinalImage()
        {
            ColorMatrix     cm = new ColorMatrix();
            ImageAttributes ia = new ImageAttributes();

            FinalGraphics.DrawImageUnscaled(MapImage, 0, 0);

            cm.Matrix33 = 0.5f;
            ia.SetColorMatrix(cm);
            FinalGraphics.DrawImage(AttributeMap, new Rectangle(Point.Empty, FinalMapImage.Size), 0, 0, FinalMapImage.Width, FinalMapImage.Height, GraphicsUnit.Pixel, ia);
            ia.ClearColorMatrix();
        }
示例#30
0
        private void DrawAttribute(int x, int y, int attribute)
        {
            ColorMatrix     cm       = new ColorMatrix();
            ImageAttributes ia       = new ImageAttributes();
            Rectangle       DestRect = new Rectangle(x * TileSize, y * TileSize, TileSize, TileSize);
            Rectangle       SrcRect  = new Rectangle((attribute % 8) * TileSize, (attribute / 8) * TileSize, TileSize, TileSize);

            FinalGraphics.DrawImage(MapImage, DestRect, DestRect, GraphicsUnit.Pixel);
            AttributeGraphics.DrawImage(AttributeBlocks, DestRect, SrcRect, GraphicsUnit.Pixel);

            cm.Matrix33 = 0.5f;
            ia.SetColorMatrix(cm);
            FinalGraphics.DrawImage(AttributeMap, DestRect, x * TileSize, y * TileSize, TileSize, TileSize, GraphicsUnit.Pixel, ia);
            ia.ClearColorMatrix();
        }