C#中級 コーディング例

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

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

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

















C#中級 コーディング例

G10)画像コントラスト補正



画像をコントラスト補正して、ビットマップで返します。弱く・元・強く


     //source:対象イメージ画像
       public static Bitmap AdjustContrast(Image source, float contrast)
       {
           // contrast: 0.0f = グレー, 1.0f = 元のコントラスト, 2.0f = 2倍
           float t = 0.5f * (1.0f - contrast);
           //ColorMatrix はこれをそのまま行列化したものです
           float[][] matrix = {
       new float[] { contrast, 0,        0,        0, 0 },
       new float[] { 0,        contrast, 0,        0, 0 },
       new float[] { 0,        0,        contrast, 0, 0 },
       new float[] { 0,        0,        0,        1, 0 },
       new float[] { t,        t,        t,        0, 1 }
           };
           var colorMatrix = new ColorMatrix(matrix);
           var attributes = new ImageAttributes();
           attributes.SetColorMatrix(colorMatrix);
           //ImageAttributes オブジェクトを、Graphics オブジェクトの DrawImage メソッドに渡します
           Bitmap outputBmp = new Bitmap(source.Width, source.Height);
           using (Graphics 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;
       }

元画像


                 弱い                        強い