通过在滚动条中滚动来显示文件夹中的图像是滞后的吗?
我有 1 个图片框和 1 个“VScrollbar”(垂直滚动条)。 当我更改滚动条的值时,我想从磁盘中的文件夹中获取图片并在图片框中显示该图像。我只想一次显示 1 张图片。
它有效,但它“滞后”,因为每次我滚动时,我都会获取滚动的索引,从我的文件夹中获取文件(取决于滚动的索引),然后制作位图并将其绑定到图片框。
private void ScreenBarScroller_ValueChanged(object sender, EventArgs e)
{
int scrollValue =
ScreenBarScroller.Value == 0 ? 0 : ScreenBarScroller.Value - 1;
ScreenClass currentScreen = ScreenList[scrollValue];
using (Bitmap newPicture = new Bitmap(
new FileStream(currentScreen.Path,
FileMode.Open, FileAccess.ReadWrite,
FileShare.Delete | FileShare.ReadWrite)))
{
pictureBox1.Image =
ResizeBitmap(newPicture, pictureBox1.Width, pictureBox1.Height);
}
}
该代码可以工作,但它太慢了,我想要“平滑”滚动。我怎样才能做到这一点?还有比 VScrollbar 更好的方法吗?
I have 1 picturebox and 1 "VScrollbar" ( vertical scrollbar ).
When i change the value of the scrollbar i want to get a picture from my folder in disk and show the image in the picturebox. I only want to show 1 picture a time.
It works but it is "laggy" because everytime i scroll, i get the index of the scroll, get the file from my folder ( depending on the index of the scroll ) and then making a bitmap and binding it to the picturebox.
private void ScreenBarScroller_ValueChanged(object sender, EventArgs e)
{
int scrollValue =
ScreenBarScroller.Value == 0 ? 0 : ScreenBarScroller.Value - 1;
ScreenClass currentScreen = ScreenList[scrollValue];
using (Bitmap newPicture = new Bitmap(
new FileStream(currentScreen.Path,
FileMode.Open, FileAccess.ReadWrite,
FileShare.Delete | FileShare.ReadWrite)))
{
pictureBox1.Image =
ResizeBitmap(newPicture, pictureBox1.Width, pictureBox1.Height);
}
}
The code works but its so damn laggy , i want the "smooth" scrolling . How can i do that? Is there a better way to do this then VScrollbar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能的解决方案之一还可以加载后台线程中接下来 5 个图像范围内的图像。因此,此时用户会转到已加载或至少正在加载的下一个图像。
另一种解决方案是在位图上使用内存映射,这会降低从磁盘读取的性能。
也可以合并这两个解决方案。
编辑
重要的是,对我来说,如果这些图像足够大,请将它们加载到另一个线程中,并通过 UI 上的消息通知用户图像正在加载,这样您就会给您的应用程序带来响应感。对于用户体验来说,没有什么比让用户不知道现在发生了什么更糟糕的了。
One of possible solutions can be load also images in the range of, let's say, next 5 images in background thread. So at the moment user goes to the next image it's already loaded or at least in loading.
Another solution can be use memory mapping on your bitmaps which will invrease performanse of reading from disk.
Can also merge these 2 solutions.
EDIT
Important is, by me, if these are big enough images, to load them in another thread and notify to the user via message on UI that image is in loading, so you will give sence of responsivness to your app. There is nothing worse for UX to leave user without a clue what is going on now.