0

我正在尝试调整亮度,但我得到的代码中的“NewBitmap”在当前上下文中不存在错误

picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);

这是我的代码

private void trackBar1_Scroll(object sender, EventArgs e)
    {
        lblBrightNum.Text = trackBar1.Value.ToString();
        picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
    }



 public static Bitmap AdjustBrightness(Bitmap Image, int Value)
    {
        Bitmap TempBitmap = (Bitmap)Image.Clone();
        float FinalValue = (float)Value / 255.0f;
        Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
        Graphics NewGraphics = Graphics.FromImage(NewBitmap);
        float[][] FloatColorMatrix ={
                  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[] {FinalValue, FinalValue, FinalValue, 1, 1}
             };

        ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
        ImageAttributes Attributes = new ImageAttributes();
        Attributes.SetColorMatrix(NewColorMatrix);
        NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
        Attributes.Dispose();
        NewGraphics.Dispose();
        return NewBitmap;

    }
4

1 回答 1

1

你的程序主要包含两个方法,一个是trackBar1_Scroll,另一个是AdjustBrightness,visual studio知道AdjustBrightness方法中的“NewBitmap”是什么,但不知道trackBar1_Scroll中的“NewBitmap”是什么

 private void trackBar1_Scroll(object sender, EventArgs e)
        {
            lblBrightNum.Text = trackBar1.Value.ToString();
            //Visual studio is complaining about this, you haven't define "NewBitmap", you can fix by adding below:
            Bitmap NewBitmap = //your bitmap
            picBox.Image = AdjustBrightness(NewBitmap, trackBar1.Value);
        }



     public static Bitmap AdjustBrightness(Bitmap Image, int Value)
        {
            Bitmap TempBitmap = (Bitmap)Image.Clone();
            float FinalValue = (float)Value / 255.0f;
            Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
            Graphics NewGraphics = Graphics.FromImage(NewBitmap);
            float[][] FloatColorMatrix ={
                      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[] {FinalValue, FinalValue, FinalValue, 1, 1}
                 };

            ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
            ImageAttributes Attributes = new ImageAttributes();
            Attributes.SetColorMatrix(NewColorMatrix);
            NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
            Attributes.Dispose();
            NewGraphics.Dispose();
            return NewBitmap;
于 2013-09-15T12:36:30.003 回答