仅在需要/可见时将图像加载到图片框中

发布于 2025-01-02 18:59:16 字数 228 浏览 9 评论 0原文

我正在编写一个显示图像缩略图的小应用程序。所有显示的图像都在同一目录中,每个图像都在它自己的组框中,带有一些标签和一个复选框。所有组框都会添加到流程布局面板中。问题是,图像的数量可能会变得相当大,而且我担心如果我加载所有图像,即使它们还不可见,内存使用/性能可能会有点失控。

有没有办法只加载用户当前可见的图像?我的第一个想法是存储框的位置并根据滚动位置确定要加载的图像,或者是否有更简单的方法来确定图片框/组框当前是否可见?

I'm programming a little application that shows thumbnails of images. All the displayed images are in the same directory, each Image is inside it's own groupbox with a few labels and a checkbox. All the group boxes get added to a flowlayoutpanel. The problem is, that the amount of images may get pretty large and I'm concerned that memory usage / performance might get a little out of hand if I load all images even if they're not yet visible.

Is there a way to load only images that are currently visible to the user? My first thought is storing the location of my boxes and determine which images to load depending on the scroll position, or is there an easier way to determine if a picturebox/groupbox is currently visible?

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

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

发布评论

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

评论(1

好听的两个字的网名 2025-01-09 18:59:16

理想情况下,您应该做的是创建缓冲区逻辑,而不是隐藏一个图像并显示另一个图像。更好的主意是在显示图像之前使用几个缓冲区来加载图像,并使用固定数量的实际字段来显示图像,而不是为每个图像设置一个新的字段。

但如果您的解决方案需要这样做,请尝试创建自定义用户控件。

尝试这样的事情:

public class customUserControl : UserControl
{
    //Store image as a Uri rather than an Image
    private Uri StoredImagePath;
    public class PictureBoxAdv : PictureBox
    {
        public PictureBoxAdv()
        {
            this.VisibleChanged +=new EventHandler(VisibleChanged);
        }
    }
    public Uri Image
    {
        get { return StoredImagePath; }
        set
        {
            StoredImagePath = value;
            if (this.Visible && StoredImagePath != null)
            {
                this.Image = Image.FromFile(StoredImagePath.AbsolutePath);
            }
        }
    }
    public void VisibleChanged(object sender, EventArgs e)
    {
        //When becomes visible, restore image, invisible, nullify.
        if (this.Visible && StoredImagePath != null)
        {
            this.Image = Image.FromFile(StoredImagePath.AbsolutePath);
        }
        else
        {
            this.Image = null;
        }
    }
}

Ideally what you should be doing is creating buffer logic rather than hiding 1 image and showing the other. It's a much better idea to have a couple buffers loading the images before you show them and have a fixed number of actual fields showing images rather than a new set per image.

But if your solution requires that, try to create a custom user control.

Try something like this:

public class customUserControl : UserControl
{
    //Store image as a Uri rather than an Image
    private Uri StoredImagePath;
    public class PictureBoxAdv : PictureBox
    {
        public PictureBoxAdv()
        {
            this.VisibleChanged +=new EventHandler(VisibleChanged);
        }
    }
    public Uri Image
    {
        get { return StoredImagePath; }
        set
        {
            StoredImagePath = value;
            if (this.Visible && StoredImagePath != null)
            {
                this.Image = Image.FromFile(StoredImagePath.AbsolutePath);
            }
        }
    }
    public void VisibleChanged(object sender, EventArgs e)
    {
        //When becomes visible, restore image, invisible, nullify.
        if (this.Visible && StoredImagePath != null)
        {
            this.Image = Image.FromFile(StoredImagePath.AbsolutePath);
        }
        else
        {
            this.Image = null;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文