2009-10-02 41 views
0

即時圖形顯示ListView中的圖像。 我想爲該圖像添加邊框。 怎麼辦?給我一個主意。在C#2008中爲列表視圖圖像設置邊框

即時通訊使用c#2008。

+0

它只是你想要一個邊框的圖像?或者它是整個列表視圖項目? – Ian 2009-10-02 08:07:37

+0

只是圍繞圖像的邊界。 – user178222 2009-10-02 08:51:30

回答

4

您可以在將圖像放入ImageList之前編輯圖像嗎? 比方說,你想的4PX黑色邊框添加到圖像 - 你可以用擴展方法實現這一目標:

/// <summary> 
/// Add a border to an image 
/// </summary> 
/// <param name="srcImg"></param> 
/// <param name="color">The color of the border</param> 
/// <param name="width">The width of the border</param> 
/// <returns></returns> 
public static Image AddBorder(this Image srcImg, Color color, int width) 
{ 
    // Create a copy of the image and graphics context 
    Image dstImg = srcImg.Clone() as Image; 
    Graphics g = Graphics.FromImage(dstImg); 

    // Create the pen 
    Pen pBorder = new Pen(color, width) 
    { 
     Alignment = PenAlignment.Center 
    }; 

    // Draw 
    g.DrawRectangle(pBorder, 0, 0, dstImg.Width, dstImg.Height); 

    // Clean up 
    pBorder.Dispose(); 
    g.Save(); 
    g.Dispose(); 

    // Return 
    return dstImg; 
} 

然後只需用類似的東西所產生的圖像添加到您的ImageList:

ImageList1.Images.Add(myImage.AddBorder(Color.Black, 4)); 
+0

非常感謝Mr.Dan – user178222 2009-10-02 08:43:51

+0

+1正是我想要建議的。 – Ian 2009-10-02 09:40:54

相關問題