2009-10-02 82 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