.NET Winform - 用户控件的缩略图

发布于 2024-07-22 18:22:28 字数 104 浏览 2 评论 0原文

我有一个使用一些用户控件的应用程序。 我想在加载用户控件以供使用时拍摄用户控件的缩略图,并将其添加到流程布局面板中。

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

I have an app which uses some usercontrols. I want to take a thumbnail image of the usercontrols when they are loaded for use and add them to a flowlayout panel.

Where can I find information on making a thumbnail image of a usercontrol when it's loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-07-29 18:22:28

我不知道在显示之前有什么方法可以做到这一点,但是一旦它出现在屏幕上,您可以使用如下方法:

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);

I don't know of a way to do it before it has been displayed, but once it is on the screen you could use an approach like this:

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;

}

You can use it to get a thumbnail of any control, like this:

myPictureBox.Image = GetControlThumb(someControl, 100);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文