WPF Snipping Tool Implementation
Last week I needed to write a snipping tool on the product. I found that the articles on the blog only had the step of capturing the screen, or required following an official account for marketing, so I wrote one myself. I found an open source framework on github today. If you are ready to start writing, you can refer to that framework.
I just finished writing it and recorded the problems I encountered to prevent forgetting them later.
@[toc]
Error when rendering canvas: System.ArgumentException: “Value does not fall within the expected range.”
1 | var renderTargetBitmap = new RenderTargetBitmap(Convert.ToInt32(_AllScreenWidth), |
Here, I originally set the rendering range according to the user frame and found an error reported. After modifying the X and Y of targetRec, the picture can be displayed. confirming that it should be the reason for the cropping range. Modifying it to render the entire screen solved the problem.
The reason for the error should be that the clipping range is consistent with the rendering range, causing an error. The original image should be guaranteed to be larger than the clipping area.
Cancel all events mounted on EventHandler
How would it be possible to remove all event handlers of the ‘Click’ event of a ‘Button’?
First of all, I actually took a detour in this step and increased the redundancy of the program. I should have created a factory pattern. When the user clicks the button, different functions are awakened according to the nature. This can be referred to the framework address above.
It was because I created four buttons under the screenshot, namely arrow, rectangle, text and save. But after waking up the arrow function, clicking the rectangle again would do both, but their click order was inconsistent, so I planned to cancel all events mounted on MouseDown, MouseUp, MouseMove every time I clicked the event button, and then re-mount to this button event, so there was the above requirement.
After searching, I found that I need to call the removeHanlder method in MouseDownEventHandler by reflection method. This can be seen in the metadata, but all current solutions are for Winform, not applicable to WPF.
This is Winform control
1 | /// <summary> |
This is WPF UIelement
1 | /// <summary> |
Arrow Style
The arrow style is also a framework found on github, I will post the address directly



