2010-02-01 12 views
9

मैं एक सिस्टम बनाना चाहता हूं। ड्रॉइंग। आइकन 32x32, 16x16 बिटमैप्स से प्रोग्रामेटिक रूप से। क्या यह संभव है? अगर मैं एक आइकन लोड करता हूं -मैं एक सिस्टम कैसे बना सकता हूं। एकाधिक आकार/छवियों के साथ ड्रॉइंग। आइकन?

Icon myIcon = new Icon(@"C:\myIcon.ico"); 

... इसमें कई छवियां हो सकती हैं।

उत्तर

7

एक फ़ाइल .ico उस में कई छवियों हो सकता है, लेकिन जब आप एक .ico फ़ाइल लोड और एक चिह्न वस्तु बनाने, उन छवियों का केवल एक ही भरी हुई है। विंडोज वर्तमान डिस्प्ले मोड और सिस्टम सेटिंग्स के आधार पर सबसे उपयुक्त छवि चुनता है और दूसरों को अनदेखा करते हुए System.Drawing.Icon ऑब्जेक्ट को प्रारंभ करने के लिए इसका उपयोग करता है।

तो आप कई छवियों के साथ System.Drawing.Icon नहीं बना सकते हैं, आपको Icon ऑब्जेक्ट बनाने से पहले आपको एक चुनना होगा।

बेशक, बिटमैप से रनटाइम पर एक आइकन बनाना संभव है, क्या आप वास्तव में क्या पूछ रहे हैं? या आप एक .ico फ़ाइल बनाने के लिए पूछ रहे हैं?

+1

साथ एक आइकन बनाना चाहता है लगता है लेकिन अगर मैं एक .ico फ़ाइल लोड है कि मैं ऊपर दिखाए गए है, मैं 16x16, 32x32, 48x48 आदि इस तरह उपयोग कर सकते हैं: - आइकन छोटा आइकन = नया आइकन (MyIcon, 16, 16); – Damien

+2

क्योंकि यह जानता है कि आइकन किस फ़ाइल/संसाधन से बनाया गया था और उस पर वापस जा सकता है और एक नई छवि प्राप्त कर सकता है। यद्यपि किसी आइकन ऑब्जेक्ट में छवि जोड़ने का कोई तरीका नहीं है। –

+0

@ जॉनकनेर वास्तव में, यह वास्तव में सच नहीं है। सभी छवि डेटा 'आइकन' वर्ग द्वारा स्मृति में लोड किया जाता है और जब आप किसी अन्य 'आइकन' ऑब्जेक्ट से आकार बदलते हैं, तो छवि डेटा साझा किया जाता है और निकटतम आकार मूल (साझा) डेटा से खींचा जाता है। – NetMage

0

एक अच्छा 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()); 
} 
+3

मैं वह एक से अधिक चित्रों ... –

0

आप .ico फ़ाइल बनाने के लिए png2ico का उपयोग करके System.Diagnostics.Process.Start का उपयोग करके इसका उपयोग कर सकते हैं। आपको अपनी छवियां बनाने और png2ico को आमंत्रित करने से पहले उन्हें डिस्क पर सहेजने की आवश्यकता होगी।

संबंधित मुद्दे