System.IO.Stream does not contain definition for CopyTo()
.net-2.0, asp.net, c#, image
Solution
Stream.CopyTo was introduced in .NET 4. Since you're targeting .Net 2.0, it's not available. Internally, `CopyTo` is mainly doing this (although has extra error handling) so you can just use this method. I've made it an extension method for convenience.
//it seems 81920 is the default size in CopyTo but this can be changed
public static void CopyTo(this Stream source, Stream destination, int bufferSize = 81920)
{
byte[] array = new byte[bufferSize];
int count;
while ((count = source.Read(array, 0, array.Length)) != 0)
{
destination.Write(array, 0, count);
}
}
So you can simply do
using (var ms = new MemoryStream())
{
fu.PostedFile.InputStream.CopyTo(ms);
ms.Position = 0;
img = new System.Drawing.Bitmap(ms);
}
Problem
Hi I am getting the error " 'System.IO.Stream' does not contain a definition for 'CopyTo' and no extension method 'CopyTo' accepting a first argument of type 'System.IO.Stream' could be found (are you missing a using directive or an assembly reference?) " I am using following lines of code in my project. ``` Bitmap img; using (var ms = new MemoryStream()) { fu.PostedFile.InputStream.CopyTo(ms); ms.Position = 0; img = new System.Drawing.Bitmap(ms); } ``` Why I am getting this error? How to solve this? Please help me...