Language/C#

C# Brightness / Saturation trackbar로 조절

비비이잉 2022. 4. 21. 11:02
반응형

Brightness / Saturation을 트랙바로 조정 

 

 

using System.Drawing.Imaging;
private static Bitmap ApplyColorMatrix(Image sourceImage, int brivalue, int satval)
        {
            //satval이 0부터 200이니까  -100을하면되겠지?

            Console.WriteLine("APPLY COLOR MATRIX");
            Console.WriteLine($"value : {brivalue}");
            
            Bitmap bmp32BppSource = GetArgbCopy(sourceImage);
            Bitmap bmp32BppDest = new Bitmap(bmp32BppSource.Width, bmp32BppSource.Height, PixelFormat.Format32bppArgb);
            float Brivalue = (float)brivalue / 255.0f;
            float saturation = (float)(satval+100) / 255.0f;

            float rWeight = 0.3086f;
            float gWeight = 0.6094f;
            float bWeight = 0.0820f;

            float a = (1.0f - saturation) * rWeight + saturation;
            float b = (1.0f - saturation) * rWeight;
            float c = (1.0f - saturation) * rWeight; //saturation이 1이면 됨 
            float d = (1.0f - saturation) * gWeight;
            float e = (1.0f - saturation) * gWeight + saturation;
            float f = (1.0f - saturation) * gWeight;//saturation이 1이면 됨 
            float g = (1.0f - saturation) * bWeight;
            float h = (1.0f - saturation) * bWeight;
            float i = (1.0f - saturation) * bWeight + saturation;
            //a~i값을 이용해 saturation 값 조정




            ColorMatrix colorMatrix = new ColorMatrix(new float[][]
                        {
                            new float[]{a, b, c, 0, 0},
                            new float[]{d, e, f, 0, 0},
                            new float[]{g, h, i, 0, 0},
                            new float[]{0, 0, 0, 1, 0},
                            new float[]{ Brivalue, Brivalue, Brivalue, 1, 1} //밝기값 조정
                        });
            using (Graphics graphics = Graphics.FromImage(bmp32BppDest))
            {
                ImageAttributes bmpAttributes = new ImageAttributes();
                bmpAttributes.SetColorMatrix(colorMatrix);

                graphics.DrawImage(bmp32BppSource, new Rectangle(0, 0, bmp32BppSource.Width, bmp32BppSource.Height),
                                    0, 0, bmp32BppSource.Width, bmp32BppSource.Height, GraphicsUnit.Pixel, bmpAttributes);
            }
            bmp32BppSource.Dispose();
            
            return bmp32BppDest;
        }
        private static Bitmap GetArgbCopy(Image sourceImage)
        {
            Bitmap bmpNew = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);


            using (Graphics graphics = Graphics.FromImage(bmpNew))
            {
                graphics.DrawImage(sourceImage, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), GraphicsUnit.Pixel);
                graphics.Flush();
            }
            return bmpNew;
        }

 

//호출해주는 부분
Bitmap bitmapimage = ApplyColorMatrix(originalImage, briTrackBar.Value, satTrackBar.Value);
Image filterimage = (Image)bitmapimage;

https://softwarebydefault.com/2013/03/03/colomatrix-image-filters/

 

C# How to: Image filtering implemented using a ColorMatrix

Article purpose This article is based around creating basic Image filters. The different types of filters discussed are: Grayscale, Transparency, Image Negative and Sepia tone. All filters are impl…

softwarebydefault.com

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-use-a-color-matrix-to-transform-a-single-color?view=netframeworkdesktop-4.8 

 

How to: Use a Color Matrix to Transform a Single Color - Windows Forms .NET Framework

Table of contents How to: Use a Color Matrix to Transform a Single Color Article 09/01/2020 3 minutes to read 1 contributor In this article --> GDI+ provides the Image and Bitmap classes for storing and manipulating images. Image and Bitmap objects store t

docs.microsoft.com

 

반응형

'Language > C#' 카테고리의 다른 글

[Magick package] 이미지 명도 채도 조정  (0) 2022.04.25
GridView Image Column 추가  (0) 2022.04.22
DataGridView 에 셀 변경 여부 체크  (0) 2022.04.14
API Error  (0) 2022.04.05
설치파일 배포 방법  (0) 2022.04.05