C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Convert an Image to Icon


Posted Date: 06 Jun 2008    Resource Type: Code Snippets    Category: Images

Posted By: shyamalendu prusty       Member Level: Silver
Rating:     Points: 5



The code sample is a function that takes an image as input and returns an Icon object. The function also takes a boolean parameter that specifies whether to keep the aspect ratio or not.


Icon MakeIcon(Image img, int size, bool keepAspRatio) {
Bitmap square = new Bitmap(size, size);
Graphics g = Graphics.FromImage(square);
int x, y, w, h;
if(!keepAspRatio|| img.Height == img.Width) {
x = y = 0;
w = h = size;
} else {
float r = (float)img.Width / (float)img.Height;
if(r > 1) {
w = size;
h = (int)((float)size / r);
x = 0; y = (size - h) / 2;
} else {
w = (int)((float)size * r);
h = size;
y = 0; x = (size - w) / 2;
}
}
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h);
g.Flush();
return Icon.FromHandle(square.GetHicon());
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Image manipulation  .  Create an icon from an image  .  Convert image to icon  .  Convert an image to icon  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Random Image selector after 1 minute
Previous Resource: How to capture/store screen shot
Return to Discussion Resource Index
Post New Resource
Category: Images


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

teleconferencing

Contact Us    Privacy Policy    Terms Of Use