.NET Winform - usercontrol的缩略图

时间:2009-06-03 04:02:18

标签: .net winforms user-controls thumbnails

我有一个使用一些用户控件的应用。我想在加载用户控件时获取用户控件的缩略图,并将它们添加到flowlayout面板。

在哪里可以找到有关在加载用户控件时制作缩略图的信息?

1 个答案:

答案 0 :(得分:2)

我不知道在显示之前有办法做到这一点,但是一旦它出现在屏幕上你可以使用这样的方法:

private Image GetControlThumb(Control control, int thumbSize)
{
    Bitmap imgLarge = new Bitmap(control.Bounds.Width, control.Bounds.Height);
    using (Graphics g = Graphics.FromImage(imgLarge))
    {
        g.CopyFromScreen(
            control.Parent.PointToScreen(new Point(control.Left, control.Top)),
            new Point(0, 0),
            new Size(control.Bounds.Width, control.Bounds.Height));
    }


    Size size;
    if (control.Width > control.Height)
    {
        size = new Size(thumbSize, (int)(thumbSize * (float)control.Height / (float)control.Width));
    }
    else
    {
        size = new Size((int)(thumbSize * (float)control.Width / (float)control.Height), thumbSize);
    }
    Image imgSmall = imgLarge.GetThumbnailImage(size.Width, size.Height, new Image.GetThumbnailImageAbort(delegate { return false; }), IntPtr.Zero);
    imgLarge.Dispose();
    return imgSmall;

}

您可以使用它来获取任何控件的缩略图,如下所示:

myPictureBox.Image = GetControlThumb(someControl, 100);
相关问题