图像加载和数据流/文件相关性 [英] Image loading and stream/file dependency

查看:158
本文介绍了图像加载和数据流/文件相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经加载了一堆缩略图到图片框控件的应用程序。有时超过一百的时间。使用图像创建Image.FromStream(MemoryStream的),和存储器流再presents每个JPG文件。

目前,只要我打电话Image.FromStream,我给的参考图片框,然后后我的flush()和Dispose()流。

据文件,我不应该放流,直到我的形象做。但是,我还没有这样做的任何错误。我的PictureBox控件可以整天重绘自己与存储流早已被处置后仍缩略图。

不过,如果我试图访问这些图像以任何方式,如调用保存功能,它抛出一个错误。

  • 如果一个位图对内存流或文件流的依赖,将其处置来源,当你处理它?或者,这是只有当它使用Bitmap.FromFile创建流本身?

  • 时的处置的MemoryStream立刻就像我现在这样算OK?该图片必须缓存图像中的某种方式。我从来没有过一个错误,因为我并不需要触摸的形象一旦被设定。

我也注意到,它需要我PictureBoxes很长的时间来画自己的父母控制。我不知道这是因为我有太多的控制,还是因为有一个延迟的第一次的图像画在一个图片。

我知道,当你创建一个新的位图文件的参数,它实际上并没有,直到它首先需要加载的文件内容。我的问题是,我不希望每次的OnPaint操作通过一个文件的读取延迟,我想,以确保数据是preloaded之前,我尝试画的控制,否则我得到闪​​烁和缓慢的画。

有没有好的办法来强制图像是preloaded?什么是Image.FromFile,Image.FromStream,新位图(文件名)等之间的差异?是否有任何这些负载的所有字节向右走,或者是延迟,直到第一次需要他们?

下面是我的code:

 的MemoryStream毫秒​​=新的MemoryStream(字节); // byte []数组
pictureBox.Image = Image.FromStream(毫秒);
ms.Flush();
ms.Dispose();
 

解决方案

GDI +这underlies图像类要求的源流保持打开状态的图像是否正常工作。这也是一个不错的主意,而图像是使用它来更改写入源流。改变应保存到一个不同的数据流。

从图片来源一切使用被窝里流。其装载行为是相同的。

图片不关闭或释放通过FromStream被提供给它的流。你必须来管理自己。

另外,主叫冲洗上的流仅具有当流已被写入到的效果。

如果你想提高你的PictureBoxes UI行为,试试这个:

  1. 设置InitialImage一个小微调的GIF。
  2. 设置WaitOnLoad为false。
  3. 指定的图像通过设置ImageLocation。 (您可以使用文件规范或它的URL。)
  4. 使用LoadAsync方法加载图像异步。

Right now I have an application that loads a bunch of thumbnail images into PictureBox controls. Sometimes over a hundred at a time. The images are created using Image.FromStream(MemoryStream), and the memory stream represents each JPG file.

Currently, as soon as I am calling Image.FromStream, I assign the reference to the picture box, then immediately after I Flush() and Dispose() the stream.

According to documentation, I should not release the stream until I'm done with the image. However, I have not had any errors doing this. My PictureBox controls can repaint themselves all day long with the thumbnail images even after the memory streams have long been disposed.

However, if I try to access these images in any way, such as calling the Save function, it throws an error.

  • If a Bitmap has a dependency on a memory stream or file stream, will it dispose of the source when you dispose of it? Or is this only when it creates the stream itself using Bitmap.FromFile?

  • Is disposing of the MemoryStream immediately like I am doing considered OK? The PictureBox must be caching the image in some way. I've never had an error because I don't need to touch the image once it has been set.

I also notice that it takes my PictureBoxes a long time to paint on their parent control. I am not sure if this is because I have too many controls, or because there is a delay the first time an image is painted in a PictureBox.

I know that when you create a new Bitmap with a file parameter, it doesn't actually load the file contents until it is first needed. My issue is, I don't want each OnPaint operation to be delayed by a file read, I want to make sure the data is preloaded before I try to paint the controls, otherwise I get flickering and slow painting.

Is there a good way to force an image to be preloaded? What are the discrepancies between Image.FromFile, Image.FromStream, new Bitmap(fileName), etc? Do any of these load all the bytes right away, or is it delayed until they are first needed?

Here's my code:

MemoryStream ms = new MemoryStream(bytes); // byte[] array
pictureBox.Image = Image.FromStream(ms);
ms.Flush();
ms.Dispose();

解决方案

GDI+ which underlies the Image classes require that the source stream remain open while the Image exists to work properly. It's also a bad idea to write changes to the source stream while Image is using it. Changes should be saved to a different stream.

Everything derived from Image uses a Stream under the covers. Their loading behaviors are the same.

Image does not Close or Dispose a stream that was provided to it via FromStream. You have to manage that yourself.

Also, calling Flush on a Stream only has an effect if the Stream has been written to.

If you're looking to improve UI behavior for your PictureBoxes, try this:

  1. Set InitialImage to a small spinner GIF.
  2. Set WaitOnLoad to false.
  3. Specify the image by setting ImageLocation. (You can use a filespec or a URL in it.)
  4. Load the images asynchronously using the LoadAsync method.

这篇关于图像加载和数据流/文件相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆