例如:
BitmapImage bitmap = new BitmapImage();
byte[] buffer = GetHugeByteArray(); // from some external source
using (MemoryStream stream = new MemoryStream(buffer, false))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
bitmap.Freeze();
}
你能告诉我任何更多关于使用
?
编辑:
正如在JaredPar的文章的评论中讨论,这个问题是更关心的是的
在VS2003的实现使用。有人指出,使用
并没有出台,直到.NET 2.0(VS2005)。 JaredPar发布了一个等效的解决方法。
As was discussed in the comments of JaredPar's post, this question is more concerned with an implementation of Using
in VS2003. It was pointed out that Using
was not introduced until .NET 2.0 (VS2005). JaredPar posted an equivalent workaround.
使用具有几乎为C#相同的语法在VB中,假设你使用.NET 2.0或更高版本(这意味着VB.NET V8编译器或更高版本) 。只是删除括号并添加结束使用
Using has virtually the same syntax in VB as C#, assuming you're using .NET 2.0 or later (which implies the VB.NET v8 compiler or later). Just remove the braces and add a "End Using"
Dim bitmap as New BitmapImage()
Dim buffer As Byte() = GetHugeByteArrayFromExternalSource()
Using stream As New MemoryStream(buffer, false))
bitmap.BeginInit()
bitmap.CacheOption = BitmapCacheOption.OnLoad
bitmap.StreamSource = stream
bitmap.EndInit()
bitmap.Freeze()
End Using
您可以在这里得到充分的文档
You can get the full documentation here
http://msdn.microsoft.com/en-us/library/htd05whh(VS.80).aspx修改
如果您使用的是VS2003或更早的版本,你需要下面的code。 using语句不引入,直到VS 2005,.NET 2.0(reference).由于克里斯!以下是等同于using语句。
If you're using VS2003 or earlier you'll need the below code. The using statement was not introduced until VS 2005, .NET 2.0 (reference). Thanks Chris!. The following is equivalent to the using statement.
Dim bitmap as New BitmapImage()
Dim buffer As Byte() = GetHugeByteArrayFromExternalSource()
Dim stream As New MemoryStream(buffer, false))
Try
bitmap.BeginInit()
bitmap.CacheOption = BitmapCacheOption.OnLoad
bitmap.StreamSource = stream
bitmap.EndInit()
bitmap.Freeze()
Finally
DirectCast(stream, IDisposable).Dispose()
End Try
上一篇:随着FileStreamResult,如何将MemoryStream关闭?如何将、FileStreamResult、MemoryStream
下一篇:CS0133"这位前pression被分配到'标识'必须是恒定QUOT; - 什么是背后的原因是什么?标识、分配、原因、QUOT