1

I have worked on image capture window application. When I have captured image by application in window tablet then image quality low and show darkness in captured image background. When I have captured image by tablet then image is good quality. What is missing/problem in my code? I have used code share by you...

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);
    }

Show you captured by application image ............

enter image description here

Show you captured by tablet ............

enter image description here

Please give me any idea and solution remove darkness captured by application (above image).

Community
  • 1
  • 1
surendra Kandira
  • 587
  • 2
  • 5
  • 22
  • just a guess: tablet image size = `3264x1826`; you set `457x350` and get a low quality image – ASh Feb 20 '16 at 08:09
  • I know, you are right but 3264x1826 is convert into 457x350 then quality not change only size change . – surendra Kandira Feb 20 '16 at 08:33
  • 1
    Are you saying... You have your app running on a Window's tablet, and the difference is with image captured by tablet's own photo software VS your software on same tablet? Just it's not clear if you're using 2 different hardwares or not. Also why tag `Flash`? Did one of the photos use a flash? Is your real question, how to enable camera flash in your own app? Otherwise you have a correct answer below. Either accept it or edit your question to be more clear on facts & goals. – VC.One Feb 20 '16 at 15:11

1 Answers1

1

You can use a DrawImage with an ImageAttributes instance to change the gamma. I found 0.5f to work:

enter image description here

Here is a function that applies a gamma value to a bitmap and returns a modified bitmap. It is up to you to ..:

  • make sure you don't leak resources
  • make sure to apply the gamma always to the original and not repeatedly to the same bitmap when giving the user a trackbar to find a good value..

The function:

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;
}

The calling code I used:

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);

If you also want to change contrast and/or brightness you can use a ColorMatrix. See here for an example!

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111