views:

222

answers:

2
+5  Q: 

Display filter C#

Hello all. It's a little hard to explain what I need but i'll try:

I need to write application (winform) which will be some kind of filter to image/other forms behind it. With one exception - all behind form should looks as is except of red (for example) color, which have to be replaced to any other specified color, white for example.

So let's imagine I have opened windows Word with few lines of text. With red and black letters. So when i place my application above this text - it should "filter" red symbols and fill them to white.

So as i understand this task: i have to snap area behind the form, then process it (replace colors) and after draw this image on my form body.

Any links or keywords for solution?

UPD:

so - this is my final solution:

  1. do form transparent (using TransparencyKey and BackColor properties)
  2. place picturebox over the form
  3. when we need to update image in picturebox - we replace current image with pictureBox1.Image = null;, then refreshing form with (this.Refresh()) and do new snapshot

thanks for all ;-)

UPD 2: sample

UPD 3: here are sources

A: 

I don't know if this can be done at all (let's see what others answer :-).

You can get a handle to the screen device context, which gives you a bitmap of the screen.

HDC dc = GetDC(NULL);

(This is C++, you'll have to use P/Invoke, or create a mixed-mode library in C++)

Then you can redraw a region of the screen with your filtering process.

Now the problems start:

  • how do you know that the pixels in your interesting region has changed ?
  • if the region changes, are the changes visible or are they hidden by your own drawing.

You could have a button somewhere that hides your own app momentarily and shows it back when re-pressed, and filters the new content.

Good luck. Any possibility of sharing the user scenario ?

Timores
in my case - pixels behind application will be static, it will simplify my solution. (i hope i can do this %)so radrawing will be necessary only in one case: my application was moved or my application get back focus after it lost it.
zerkms
That's a good simplification. I think @Thorsten's solution is better (no C++).
Timores
+3  A: 

Hi, you can create a snapshot of the desktop using the following code:

public Bitmap CaptureScreen()
{
    Bitmap b = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
    Graphics g = Graphics.FromImage(b);
    g.CopyFromScreen(0, 0, 0, 0, b.Size);
    g.Dispose();
    return b;
}

Replace the dimensions and position with the coordinates of your form. This way you get a bitmap of what's behind your form. Then you can do the color replacement on that bitmap.

Please note that due to settings like ClearType and other anti-aliasing mechanisms, you have to also take into account "intermediate pixels" when doing the color replacement. Otherwise things will look funny :-)

Thorsten Dittmar
yep, i already thought about "near" colors. this would be a next challenge ;-)so - can anyone propose better solution?
zerkms
Great ! I didn't know the CopyFromScreen method, exactly why I thought @zerkms had to resort to C++
Timores