在winform c中捕获图像背景时消除黑暗

时间:2016-02-20 07:35:58

标签: c# image winforms flash

我曾参与过图像捕获窗口应用程序。当我通过窗口平板电脑中的应用程序捕获图像时,图像质量低并且在捕获的图像背景中显示黑暗。当我通过平板电脑拍摄图像时,图像质量很好。  我的代码中缺少什么/问题? 我已经使用过您的代码共享...

private void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
           Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();                     

         ImgContainer.Image = b;


        }
    private void btnKeep_Click(object sender, EventArgs e)
            {
    int width = 457;
                int height = 350;

                Image tmpimg = ImgContainer.Image;                
                System.Drawing.Bitmap b = new System.Drawing.Bitmap(ImgContainer.Image, width, height);
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(b);
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);
                System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
                System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
                eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                string ImagePath = Guid.NewGuid().ToString();
                string imagefullpath = System.AppDomain.CurrentDomain.BaseDirectory + "imageFolder\\" + ImagePath + ".jpg";                  
                b.Save(imagefullpath);
    }

显示您通过应用程序图像捕获............

Tab Content Script (v 2.2)

显示您通过平板电脑拍摄............

enter image description here

请给我任何想法和解决方案,消除应用程序捕获的黑暗(上图)。

1 个答案:

答案 0 :(得分:1)

您可以使用DrawImageImageAttributes实例来更改gamma。我发现0.5f可以工作:

enter image description here

这是一个将伽玛值应用于位图并返回修改后的位图的函数。这取决于你......:

  • 确保您没有泄漏资源
  • 确保将gamma始终应用于原始,而不是重复应用于相同的位图,同时为用户提供轨迹栏以找到合适的值。

功能:

public static Bitmap ApplyGamma(Bitmap bmp0, float gamma)
{
    Bitmap bmp1 = new Bitmap(bmp0.Width, bmp0.Height);
    using (Graphics g = Graphics.FromImage(bmp1))
    {
        ImageAttributes attributes = new ImageAttributes();
        attributes.SetGamma(gamma, ColorAdjustType.Bitmap);
        g.DrawImage(bmp0, new Rectangle(0, 0, bmp0.Width, bmp0.Height),
                    0, 0, bmp0.Width, bmp0.Height, GraphicsUnit.Pixel, attributes);
    }
    return bmp1;
}

我使用的调用代码:

Image img = Image.FromFile(yourImage);            // some image to use
float gamma = (float)(trackBar1.Value / 10f);     // a trackbar to test
Text = "Gamma = " + gamma;                        // a control display
pictureBox1.Image = ApplyGamma((Bitmap)img, gamma);

如果您还想更改对比度和/或亮度,可以使用ColorMatrix。有关示例,请参阅here