0

I am trying to set the threshold of an image at runtime. Currently my code looks like:

ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetThreshold(kryptonTrackBar1.Value / 100);

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(pictureBox3.Image);
Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0,
                 bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr);
pictureBox1.Image = bmp;

picturebox3 contains the black and white image. The problem is inside the imageAttr.SetThreshold(kryptonTrackBar1.Value / 100); line.

My trackbar max is 100 and min 0. So I should end up with numbers like 0.07 and so on while scrolling the trackbar, but the trackbar only returns the value of 0 and 1! 1 if I scroll all the way to the right!

What's wrong with it?

Mat
  • 202,337
  • 40
  • 393
  • 406
Euro2012
  • 3
  • 2

1 Answers1

3

How about

imageAttr.SetThreshold((float)kryptonTrackBar1.Value / 100.0f);
Jaska
  • 1,412
  • 1
  • 18
  • 39
  • Thats it!! thank your very much! i am new to c#(vb.net background). – Euro2012 Jun 26 '12 at 20:26
  • Yes. You tried dividing integer value 1 - 100 by integer value 100, which results always in 0 or 1. They must be casted to floats so that .NET understands you need a floating point division. – Jaska Jun 26 '12 at 20:28