Most Photoshop people would know what this does, basically it re-sizes the image removing all empty space.
Is there a function in GDI+ that does something similar? Or do I have to write my own? Or if somebody has one already that would be nice.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Actually, writing this wouldn’t be too terribly difficult. Being that I use Photoshop daily to design websites, and my application graphics for my own software I develop, Trim bases it off of a pixel color you select. You could do the calculation in four rectangular passes by navigating through the rows of pixels comparing their color to the color you wish to trim, storing the cordinates you are at along the way. Let me explain.
Essentially what you are doing is navigating through the pixels from left to right, top to bottom, generating a Rectangle object that stores the region of (in this case) White pixels. This would be done in four passes, so you would have generated four Rectangle regions you would clip from the image.
The logic behind the calculation is as follows (in semi-psuedo code C#)
You would essentially repeat this algorithm for 4 passes (4 sides of the image), until you have trimmed all the whitespace (or whatever color you need). This should work for non-traditional shaped images with scattered colors as well because of the algorithm. It stops calculating the region as soon as it detects a non-white pixel, and you should then clip that region, and perform another pass. Rinse and repeat.
Note that I have not tested this; it is all in theory, but I wanted to provide you a general idea or approach to this other then just linking to a 3rd Party Library or component.
Edit: