ObjectDisposedException when I try to use an Image source

Nick

I need to add an Image to my Panel, so I use the following code:

var image = new Image();
var source = new BitmapImage();
source.BeginInit();
source.CacheOption = BitmapCacheOption.OnLoad;
source.StreamSource = new FileStream(filename, FileMode.Open);
source.EndInit();

// I close the StreamSource so I can load again the same file
source.StreamSource.Close();
image.Source = source;

The problem is that when I try to use my image source I get an ObjectDisposedException:

var source = ((BitmapImage)image.Source).StreamSource;

// When I use source I get the exception
using (var stream = new MemoryStream((int)(source.Length)))
{
    source.Position = 0;
    source.CopyTo(stream);
    // ...
}

It happens because I closed the source, but if I don't close it I can't be able to load again the same file.

How can I solve this problem (i.e. close the source to be able to load the same file more than once, and to be able to use the source without get the exception)?

gliderkite

The following solution should work for you:

var image = new Image();
var source = new BitmapImage();
source.BeginInit();
source.CacheOption = BitmapCacheOption.OnLoad;

// Create a new stream without disposing it!
source.StreamSource = new MemoryStream();

using (var filestream = new FileStream(filename, FileMode.Open))
{
   // Copy the file stream and set the position to 0
   // or you will get a FileFormatException
   filestream.CopyTo(source.StreamSource);
   source.StreamSource.Position = 0;
}

source.EndInit();
image.Source = source;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Error when I try playground for Swift in Xcode 6 beta

来自分类Dev

Why don't I get correct result when I use original parseList function in Kotlin?

来自分类Dev

Should I use a pointer when dealing with lists?

来自分类Dev

System cannot find the file specified when I try to call an executable file in C#

来自分类Dev

Trigger problem, it compiles fine, but when i try to update data it does not triggerr the text message

来自分类Dev

Try and catch blocks: whether in the class itself or when I call the method outside the class

来自分类Dev

Why does the kernel restart when I try sklearn PCA?

来自分类Dev

Safely storing credentials when I need to retrieve the password for use

来自分类Dev

未显示位图的Image.Source

来自分类Dev

How can I request an Image from a URL when the Response is 302?

来自分类Dev

"No, missing feature: WATCH" when I try to run my smartphone app with wear app?

来自分类Dev

When should I use val x = fn as opposed to fun x

来自分类Dev

Why is my Yesod app throwing a TlsNotSupported exception when I try to log in?

来自分类Dev

Use base64 svg image as css content source

来自分类Dev

UITableView is being reset when I use Custom Class

来自分类Dev

How to get Image.Source as string

来自分类Dev

正确避免ObjectDisposedException

来自分类Dev

HttpClient上的ObjectDisposedException

来自分类Dev

ImageButton: Mipmap source image not stretching to fill button

来自分类Dev

ObjectDisposedException:CancellationTokenSource已被处置

来自分类Dev

React.js Cant replace image source when image throws a 500 error

来自分类Dev

<Image source = {require(“”)} />不能正确显示?

来自分类Dev

<Image source = {require(“”)} />不能正确显示?

来自分类Dev

<Image source = {require(“”)} />不能正确显示?

来自分类Dev

<Image source = {require(“”)} />不能正确显示?

来自分类Dev

动画,Image.Source和绑定

来自分类Dev

ObjectDisposedException:CancellationTokenSource已被处置

来自分类Dev

将 BitmapImage 转换为 Image.Source

来自分类Dev

扩展 UserManager 导致 ObjectDisposedException

Related 相关文章

  1. 1

    Error when I try playground for Swift in Xcode 6 beta

  2. 2

    Why don't I get correct result when I use original parseList function in Kotlin?

  3. 3

    Should I use a pointer when dealing with lists?

  4. 4

    System cannot find the file specified when I try to call an executable file in C#

  5. 5

    Trigger problem, it compiles fine, but when i try to update data it does not triggerr the text message

  6. 6

    Try and catch blocks: whether in the class itself or when I call the method outside the class

  7. 7

    Why does the kernel restart when I try sklearn PCA?

  8. 8

    Safely storing credentials when I need to retrieve the password for use

  9. 9

    未显示位图的Image.Source

  10. 10

    How can I request an Image from a URL when the Response is 302?

  11. 11

    "No, missing feature: WATCH" when I try to run my smartphone app with wear app?

  12. 12

    When should I use val x = fn as opposed to fun x

  13. 13

    Why is my Yesod app throwing a TlsNotSupported exception when I try to log in?

  14. 14

    Use base64 svg image as css content source

  15. 15

    UITableView is being reset when I use Custom Class

  16. 16

    How to get Image.Source as string

  17. 17

    正确避免ObjectDisposedException

  18. 18

    HttpClient上的ObjectDisposedException

  19. 19

    ImageButton: Mipmap source image not stretching to fill button

  20. 20

    ObjectDisposedException:CancellationTokenSource已被处置

  21. 21

    React.js Cant replace image source when image throws a 500 error

  22. 22

    <Image source = {require(“”)} />不能正确显示?

  23. 23

    <Image source = {require(“”)} />不能正确显示?

  24. 24

    <Image source = {require(“”)} />不能正确显示?

  25. 25

    <Image source = {require(“”)} />不能正确显示?

  26. 26

    动画,Image.Source和绑定

  27. 27

    ObjectDisposedException:CancellationTokenSource已被处置

  28. 28

    将 BitmapImage 转换为 Image.Source

  29. 29

    扩展 UserManager 导致 ObjectDisposedException

热门标签

归档