Well first of all, you will need to load a bitmap, similar to:
http://www.gamedev.net/reference/articles/article1966.aspNext you want to compute the bounding box of the subimage. You can achieve that by looping through every pixel and recording the left-most, top-most, bottom-most and right-most non-transparent pixel. An easy way is to make 4 variables, for ex. xMin, xMax, yMin, yMax, then initialize them with large values such that xMin and yMin will contain a large positive value and xMax and yMax will contain a large negative value. While looping through each pixel, compare the current position with respect to each of the variables and update them accordingly. So, for eg. while looping in the x direction, you can check if the current x position is < xMin, and if so, we can set xMin to the current x, and so on..
Once you got the bounding rectangle, you just extract the pixels and save them to file.
(Also, in case you didn't know, Photoshop's "Trim..." function or GIMP's Auto Crop function does what you want, but perhaps you have a different reason for doing it in code)