0

I am trying to write a program. It will work at the background and it will constantly capture the screen, blur the image after that it will give me color of a pixel that I want. I tried to do this with that code. But it give me pixel color without blur effect. How can I get the blurry pixel color?

Screenshot, blur and get pixel function:

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.Hide();
    Bitmap foto = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    BlurBitmapEffect myBlurEffect = new BlurBitmapEffect();
    System.Threading.Thread.Sleep(200);
    Graphics grafik = Graphics.FromImage(foto);
    grafik.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(foto.Width, foto.Height));
    imgBox.Source = BitmapToImageSource(foto);
    myBlurEffect.Radius = myBlurValue;
    imgBox.BitmapEffect = myBlurEffect;
    colorValue.Text = foto.GetPixel(foto.Width / 2, foto.Height / 2).ToString();
    this.Show();
}

XAML file:

<Image Name="imgBox" HorizontalAlignment="Left" Height="419" VerticalAlignment="Top" Width="792" ></Image>
<Button Content="Take ScreenShot" HorizontalAlignment="Left" Margin="674,389,0,0" VerticalAlignment="Top" Width="108" Cursor="Hand" Click="Button_Click"/>
<Button Name="blurDown" Content="-" HorizontalAlignment="Left" Margin="10,394,0,0" VerticalAlignment="Top" Width="75" Cursor="Hand" Click="Button_Click_Down"/>
<Button Name="blurUp" Content="+" HorizontalAlignment="Left" Margin="90,394,0,0" VerticalAlignment="Top" Width="75" Cursor="Hand" Click="Button_Click_Up"/>
<TextBox Name="blurValue" HorizontalAlignment="Center" Height="23" Margin="338,386,334,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextAlignment="Center" FontWeight="Bold" Background="{x:Null}" Foreground="White" BorderBrush="{x:Null}">
    <TextBox.Effect>
        <DropShadowEffect/>
    </TextBox.Effect>
</TextBox>
<TextBox Name="colorValue" HorizontalAlignment="Center" Height="23" Margin="261,10,257,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="274" TextAlignment="Center" FontWeight="Bold" Background="{x:Null}" Foreground="White" BorderBrush="{x:Null}">
    <TextBox.Effect>
        <DropShadowEffect/>
    </TextBox.Effect>
</TextBox>
  • What is the reason behind trying to blur the bitmap? Are you trying to get something like an average color? – Clemens Oct 20 '20 at 16:55
  • Yes I am trying to get an average color . I will send that color to neopixel leds. – Oğuz Kağan Oct 20 '20 at 17:32
  • This will give you a bitmap with a single, averaged pixel, when you set the scale to `1d / bitmap.PixelWidth`: https://stackoverflow.com/questions/18189501/create-thumbnail-image-directly-from-header-less-image-byte-array. You should be able to do something similar directly with the WinForms Bitmap, e.g. like this: https://stackoverflow.com/a/10445101/1136211 – Clemens Oct 20 '20 at 17:56
  • You could do a box blur in code on the pixels rather than rendering offscreen. One advantage would be you could pick just the part of the picture you're interested in. Box blur uses a weighted average across a set of cells. You can run that averaging vertically then, on the results, horizontally. You do this for r then g then b. I could go find my 2d code if you like. I spent some time looking at a load of really complicated examples before i found an approximate version was easier to understand. ( My use case is a height map.) – Andy Oct 20 '20 at 18:02

0 Answers0