एक अच्छा code snippet here है। यह Icon.FromHandle
विधि का उपयोग करता है।
लिंक से:
/// <summary>
/// Converts an image into an icon.
/// </summary>
/// <param name="img">The image that shall become an icon</param>
/// <param name="size">The width and height of the icon. Standard
/// sizes are 16x16, 32x32, 48x48, 64x64.</param>
/// <param name="keepAspectRatio">Whether the image should be squashed into a
/// square or whether whitespace should be put around it.</param>
/// <returns>An icon!!</returns>
private Icon MakeIcon(Image img, int size, bool keepAspectRatio) {
Bitmap square = new Bitmap(size, size); // create new bitmap
Graphics g = Graphics.FromImage(square); // allow drawing to it
int x, y, w, h; // dimensions for new image
if(!keepAspectRatio || img.Height == img.Width) {
// just fill the square
x = y = 0; // set x and y to 0
w = h = size; // set width and height to size
} else {
// work out the aspect ratio
float r = (float)img.Width/(float)img.Height;
// set dimensions accordingly to fit inside size^2 square
if(r > 1) { // w is bigger, so divide h by r
w = size;
h = (int)((float)size/r);
x = 0; y = (size - h)/2; // center the image
} else { // h is bigger, so multiply w by r
w = (int)((float)size * r);
h = size;
y = 0; x = (size - w)/2; // center the image
}
}
// make the image shrink nicely by using HighQualityBicubic mode
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
g.Flush(); // make sure all drawing operations complete before we get the icon
// following line would work directly on any image, but then
// it wouldn't look as nice.
return Icon.FromHandle(square.GetHicon());
}
साथ एक आइकन बनाना चाहता है लगता है लेकिन अगर मैं एक .ico फ़ाइल लोड है कि मैं ऊपर दिखाए गए है, मैं 16x16, 32x32, 48x48 आदि इस तरह उपयोग कर सकते हैं: - आइकन छोटा आइकन = नया आइकन (MyIcon, 16, 16); – Damien
क्योंकि यह जानता है कि आइकन किस फ़ाइल/संसाधन से बनाया गया था और उस पर वापस जा सकता है और एक नई छवि प्राप्त कर सकता है। यद्यपि किसी आइकन ऑब्जेक्ट में छवि जोड़ने का कोई तरीका नहीं है। –
@ जॉनकनेर वास्तव में, यह वास्तव में सच नहीं है। सभी छवि डेटा 'आइकन' वर्ग द्वारा स्मृति में लोड किया जाता है और जब आप किसी अन्य 'आइकन' ऑब्जेक्ट से आकार बदलते हैं, तो छवि डेटा साझा किया जाता है और निकटतम आकार मूल (साझा) डेटा से खींचा जाता है। – NetMage