I became intrigued by the problem of finding the color at any point of a rectangle that has been filled with a LinearGradientBrush. There is are techniques for doing this by essentially sampling the pixel at a rendered point but I wanted to approach the problem algorithmically.
I developed the following method which receives a rectangle object that has been filled with a LinearGradientBrush and a point relative to the upper left corner of the rectangle and returns the calculated color at that point. There are no restrictions on the Start/Stop Points or the number of GradientStops. The ColorInterpolationMode can be either SRgbLinearInterpolation (default) or ScRgbLinearInterpolation. The GradientSpreadMethod is required to be the default (Pad).
There are two methods below:
Color GetColorAtPoint(Rectangle r, Point p) – finds the color at any point of a LinearGradientBrush filled rectangle.
private Double dist(Point px, Point po, Point pf) – helper method for GetColorAtPoint.
To use, copy both methods to your class and call GetColorAtPoint() with a rectangle and point.
The method works by projecting the input point onto the vector that is described by the LinearGradientBrush Start/Stop points. There are special cases for horizontal and vertical lines. The width of the line is calculated as well as the relative distance of the projected point from the StartPoint (using the dist() method). The GradientStops are ordered and two stops are found that are the closest the projected point. The color channel values are interpolated based on the relative position of the projected point and these two stops. The values are assembled into a color and returned.
You can download a demo of this method here.
Apparently, .Net does not provide a good set of classes to create icons programmatically. In helping someone on a forum, I ran across a free tool for image processing that does a great job.
Here are the steps:
1. Download the FreeImage .dll from here.
2. Copy the .dll into the bin/Debug and/or bin/Release folder in your project
3. Add an Enum and FreeImage Class to your project (shown in the example below).
4. Use three calls to the functions in FreeImage: load the bitmap file, save in icon format, release the resource.
The following example loads a .bmp into FreeImage and saves it back as a .ico file. It includes the required Enum and FreeImage Class. (Requires using System.Runtime.InteropServices; )
Visual Studio has a powerful array of features that can greatly boost productivity–especially for new programmers who may not have a full grasp of the .Net framework. While the C# language itself is quite compact and comprehensible, the number of classes available in the .Net framework is almost mind boggling. How is a newcomer supposed to remember it all?
The good news is that with Visual Studio you don’t have to. You just need to know how to get the information (‘411’) when you need it. Here are the most helpful features for me:
There are three big productivity enhancements that I get out of Intellesense. First, I use it to help find out what is possible with an object. By typing a ‘.’ after the object name in a statement, the Intellesense drop down shows all of the properties and methods that are available to me. Icons by the names of the members indicate the type (properties: a page with a hand, methods: a diamond, events: a lightning bolt). Second, selecting one of the members gives a brief synopsis which includes the type of value returned and the arguments that are expected. After selecting, if I type another delimiter (a space, a dot, a parenthesis), Intellesense will enter the code for me reducing the possibility of making a typographical mistake. For methods, entering the open parenthesis results in Intellesense showing the possible overloads (different argument possibilities). Intellesense is almost like having an expert programmer sitting over your shoulder advising you every step of the way.
Dynamic help is a way of quickly getting to more information from the MSDN documentation file. Turn this feature on using menu Help > Dynamic Help. Now, when you click on an item in the code view or the designer view, a list of links to the Help documentation is presented. Click on a link and most likely you will get exactly the information you are seeking.
Newcomers need to understand the format of the help documentation so as not to be overwhelmed by its scope. Every class typically has the following organization: an overview page, a members page listing all properties, methods and events, finally leading to pages for individual members. More often than not, the individual members page will offer an example that can be adapted or even copied directly into the application.
For me, the most useful way of getting to the information in the documentation (outside of dynamic help) is to use the Index: menu Help > Index. Also available are ‘Search’ and ‘Content’ but ‘Index’ works best for me.
How do I
Help > How do I can also lead to helpful articles in the documentation for doing the most common tasks like File IO or drawing.
Getting Help Online
Outside of Visual Studio there is a wealth of information. If I have a question about a control or how to do something, chances are good that someone else has had the same problem and has already posted the solution. I usually fire up a search engine and put in a few keywords about the problem along with ‘msdn’. This biases the search to either the msdn documentation or one of the msdn forums.
For me, asking a question in an online forum is my option of last resort. Not that it is a bad thing to do but the time I spend using the other resources available is always a valuable learning experience in itself and can turn up nuggets that can be used in later projects.