C#中級 コーディング例

G1)滑らかサインウェーブ
G2)単一色ペイント
   (スタック方式)
G3)
ギザトゲ楕円
G4)ナミモク四角
G5)画像半透明化
G6)画像セピア化
G7)画像グレー化
G8)画像明度補正
G9)画像彩度補正
G10)画像コントラスト補正

C1)マウス移動可能パネル

この講座は中級者用です。
「using」「Form」等は、省略しています。

















C#中級 コーディング例

G8)画像明度補正



画像を明度補正して、ビットマップで返します。暗く・元・明るく


     //source:対象イメージ画像
       public static Bitmap AdjustBrightness(Image source, float brightness)
       {
           // brightness: -1.0f 〜 +1.0f(0 で変化なし)
      //
           float b = brightness;
           //ColorMatrix はこれをそのまま行列化したものです
           float[][] ptsArray = {
       new float[] {1, 0, 0, 0, 0},
       new float[] {0, 1, 0, 0, 0},
       new float[] {0, 0, 1, 0, 0},
       new float[] {0, 0, 0, 1, 0},
       new float[] {b, b, b, 0, 1}
           };
           var colorMatrix = new ColorMatrix(ptsArray);
           var attributes = new ImageAttributes();
           attributes.SetColorMatrix(colorMatrix);
           //ImageAttributes オブジェクトを、Graphics オブジェクトの DrawImage メソッドに渡します
           var outputBmp = new Bitmap(source.Width, source.Height);
           using (var g = Graphics.FromImage(outputBmp))
           {
               g.DrawImage(source,
                   new Rectangle(0, 0, outputBmp.Width, outputBmp.Height),
                   0, 0, source.Width, source.Height,
                   GraphicsUnit.Pixel,
                   attributes);
           }
           return outputBmp;
       }

元画像


                 暗い                        明るい