To get the entire buffer, use the GetBuffer method. This method returns a copy of the contents of the MemoryStream as a byte array. If the current instance was constructed on a provided byte array, a copy of the section of the array to which this instance has access is returned.
The HttpPostedFile class provides properties and methods to get information about an individual file and to read and save the file. The HtmlInputFile control can be used to select and upload files from a client. Files are uploaded in MIME multipart/form-data format.
In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file. Example: Java.
Read the image using the read() method of the ImageIO class. Create a ByteArrayOutputStream object. Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class. Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.
Use a BinaryReader object to return a byte array from the stream like:
byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes(file.InputStream.Length);
line 2 should be replaced with
byte[] binData = b.ReadBytes(file.ContentLength);
It won't work if your file InputStream.Position is set to the end of the stream. My additional lines:
Stream stream = file.InputStream;
stream.Position = 0;
in your question, both buffer and byteArray seem to be byte[]. So:
ImageElement image = ImageElement.FromBinary(buffer);
before stream.copyto, you must reset stream.position to 0; then it works fine.
For images if your using Web Pages v2 use the WebImage Class
var webImage = new System.Web.Helpers.WebImage(Request.Files[0].InputStream);
byte[] imgByteArray = webImage.GetBytes();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With