【问题标题】:.NET Winform - thumbnail of usercontrol.NET Winform - 用户控件的缩略图
【发布时间】:2010-10-30 21:21:33
【问题描述】:

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

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

【问题讨论】:

    标签: .net winforms user-controls thumbnails


    【解决方案1】:

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

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

    【讨论】:

    • 点击按钮我可以将图像保存到磁盘但在用户控制上我得到一个白色图像,我可以使用 Paint()... 我知道这被调用了好几次一个用户控件
    • Using 块的用途是什么?
    • Graphics 类实现 IDisposable; using 块确保调用 Dispose 方法,以便 Graphics 对象可以释放资源。
    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多