Using aspose, I have converted the first page of a pdf document to a jpeg (to be used as a thumbnail in a 'Documents' section to one of my asp.net pages). This is, upto this point, stored in a FileStream - but I need a byte array to assign to the datavalue of an Image control. Can anyone point me in the right direction to converting this? I've had a good look around and I can't find the solution.
Thanks a lot.
This should work:
byte[] data = File.ReadAllBytes("path/to/file.jpg")
var memStream = new MemoryStream();
yourFileStream.CopyTo(memStream);
var bytes = memStream.ToArray();
you can try this....
/// <summary>
/// Function to get byte array from a file
/// </summary>
/// <param name="_FileName">File name to get byte array</param>
/// <returns>Byte Array</returns>
public byte[] FileToByteArray(string _FileName)
{
byte[] _Buffer = null;
try
{
// Open file for reading
System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// attach filestream to binary reader
System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
// get total byte length of the file
long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
// read entire file into buffer
_Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
// close file reader
_FileStream.Close();
_FileStream.Dispose();
_BinaryReader.Close();
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
return _Buffer;
}
Related
i'm trying to decompress a file in memory using GZipStream, copy the decompressed data to a MemoryStream, and then read the MemoryStream using BinaryReader (from Unity 3d). However, i get these errors when i try to run it:
EndOfStreamException: Failed to read past end of stream.
System.IO.BinaryReader.FillBuffer (Int32 numBytes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:119)
System.IO.BinaryReader.ReadInt32 () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:432)
LoadNii.LoadNifti (System.String fullPath, Boolean loadFromResources) (at Assets/scripts/LoadNii.cs:240)
opengl_main.PrepareNewAnatomy (System.String fullPath, Boolean loadFromResources) (at Assets/scripts/opengl_main.cs:193)
opengl_main.LoadFileUsingPath () (at Assets/scripts/opengl_main.cs:656)
UnityEngine.Component:SendMessage(String, Object)
SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SendCallbackMessage(String) (at Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:274)
SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SelectFile() (at Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:267)
UnityEngine.EventSystems.EventSystem:Update()
anyone know what the problem is? is there another way to unzip a file in memory, and transfer it to a binaryReader? thanks
code:
Stream stream = null;
if (loadFromResources == true)
{
TextAsset textAsset = Resources.Load(fullPath) as TextAsset;
Debug.Log(textAsset);
stream = new MemoryStream(textAsset.bytes);
}
else
{
FileInfo fi1 = new FileInfo(fullPath);
if (fi1.Extension.Equals(".gz"))
{
stream = new MemoryStream();
byte[] buffer = new byte[4096];
using (Stream inGzipStream = new GZipStream(File.Open(fullPath,FileMode.Open), CompressionMode.Decompress))
{
int bytesRead;
while ((bytesRead = inGzipStream.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, bytesRead);
}
}
}
else
stream = File.Open(fullPath, FileMode.Open);
}
using (BinaryReader reader = new BinaryReader(stream))
{
//header headerKey substruct:
headerKey.sizeof_hdr = reader.ReadInt32(); //ERROR
}
Each time you write to a stream, its Position is increased.
Just set stream.Position = 0 after writing to it, so that you'll start reading from the first byte again afterwards.
This is just a function that deserializes and get your result back. Thank you #C.Evenhuis.
/// <summary>
///Get data from a binary file.
/// </summary>
/// <param name="filename"></param>
/// <param name="path"></param>
/// <returns>Null if no object is found </returns>
public static T GetData<T>(string filename, string path)
{
//Path is empty.
if (string.IsNullOrEmpty(path)) throw new NullReferenceException("Path can not be null or empty");
//file is empty.
if (string.IsNullOrEmpty(filename)) throw new NullReferenceException("File name can not be null or empty.");
//Storage path
var storagePath = Path.Combine(path, filename);
//check file not exist
if (!File.Exists(storagePath))
throw new NullReferenceException("No file with the name " + filename + "at location" + storagePath);
else
try
{
//create new binary formatter .
BinaryFormatter binaryFormatter = new BinaryFormatter();
//Read the file.
using (FileStream fileStream = File.Open(storagePath, FileMode.Open))
{
//result .
var result = binaryFormatter.Deserialize(fileStream);
//Set file stream to zero .
//to avoid Exception: FailedSystem.IO.EndOfStreamException:
//Failed to read past end of stream.
fileStream.Position = 0;
//Check if casting is possible and raise error accordin to that .
return (T)Convert.ChangeType(binaryFormatter.Deserialize(fileStream), typeof(T));
}
}
catch (Exception ex)
{
throw new Exception("Failed" + ex);
}
}
I have this code:
public static List<ReplicableObject> ParseStreamForObjects(Stream stream)
{
List<ReplicableObject> result = new List<ReplicableObject>();
while (true)
{
// HERE I want to check that there's at least four bytes left in the stream
BinaryReader br = new BinaryReader(stream);
int length = br.ReadInt32();
// HERE I want to check that there's enough bytes left in the stream
byte[] bytes = br.ReadBytes(length);
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
result.Add((ReplicableObject) Formatter.Deserialize(ms));
ms.Close();
br.Close();
}
return result;
}
Unfortunately, the stream object is always going to be a TCP stream, which means no seek operations. So how can I check to make sure that I'm not over-running the stream where I've put the // HERE comments?
I don't think there's any way to query a NetworkStream to find the data you're looking for. What you'll probably need to do is buffer whatever data the stream makes available into another data structure, then parse objects out of that structure once you know it's got enough bytes in it.
The NetworkStream class provides a DataAvailable property that tells you if any data is available to be read, and the Read() method returns a value indicating how many bytes it actually retrieved. You should be able to use those values to do the buffering you need.
See Mr. Skeets page
Sometimes, you don't know the length of the stream in advance (for instance a network stream) and just want to read the whole lot into a buffer. Here's a method to do just that:
/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
public static byte[] ReadFully (Stream stream)
{
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read (buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write (buffer, 0, read);
}
}
}
This should give you some ideas. Once you have the byte array, checking the Length will be easy to do.
In your example, it would look something like this:
int bytes_to_read = 4;
byte[] length_bytes = new byte[bytes_to_read];
int bytes_read = stream.Read(length_bytes, 0, length_bytes.Length);
// Check that there's at least four bytes left in the stream
if(bytes_read != bytes_to_read) break;
int bytes_in_msg = BitConverter.ToInt32(length_bytes);
byte[] msg_bytes = new byte[bytes_in_msg];
bytes_read = stream.Read(msg_bytes, 0, msg_bytes.Length);
// Check that there's enough bytes left in the stream
if(bytes_read != bytes_in_msg ) break;
...
I can't figure out how to use FileStream to write data to a text file...
Assuming you have the data already:
string path = #"C:\temp\file"; // path to file
using (FileStream fs = File.Create(path))
{
// writing data in string
string dataasstring = "data"; //your data
byte[] info = new UTF8Encoding(true).GetBytes(dataasstring);
fs.Write(info, 0, info.Length);
// writing data in bytes already
byte[] data = new byte[] { 0x0 };
fs.Write(data, 0, data.Length);
}
(taken from msdn docs and modified)
The documentation for FileStream gives an excellent example.
In short you create a filestream object, and use the Encoding.UTF8 object (or the encoding you want to use) to convert your plaintext to bytes, in which you can use your filestream.write method.
But it would be easier to just use the File class, and File.Append* methods.
EDIT: Example
File.AppendAllText("/path/to/file", "content here");
From MSDN:
FileStream fs=new FileStream("c:\\Variables.txt", FileMode.Append, FileAccess.Write, FileShare.Write);
fs.Close();
StreamWriter sw=new StreamWriter("c:\\Variables.txt", true, Encoding.ASCII);
string NextLine="This is the appended line.";
sw.Write(NextLine);
sw.Close();
http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
Assuming your data is string based, this works well, changed your exception handling as you see fit. Making sure add a using System.IO for TextWriter and StreamWriter references.
using System.IO;
/// <summary>
/// Writes a message to the specified file name.
/// </summary>
/// <param name="Message">The message to write.</param>
/// <param name="FileName">The file name to write the message to.</param>
public void LogMessage(string Message, string FileName)
{
try
{
using (TextWriter tw = new StreamWriter(FileName, true))
{
tw.WriteLine(DateTime.Now.ToString() + " - " + Message);
}
}
catch (Exception ex) //Writing to log has failed, send message to trace in case anyone is listening.
{
System.Diagnostics.Trace.Write(ex.ToString());
}
}
using (var fs = new FileStream(textFilePath, FileMode.Append))
using (var sw = new StreamWriter(fs))
{
sw.WriteLine("This is the appended line.");
}
This question already has answers here:
Convert System.Windows.Media.ImageSource to ByteArray
(1 answer)
Convert byte array to image in wpf
(3 answers)
convert array of bytes to bitmapimage
(2 answers)
Closed 7 months ago.
Can anybody suggest how I can convert an image to a byte array and vice versa?
I'm developing a WPF application and using a stream reader.
Sample code to change an image into a byte array
public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms,imageIn.RawFormat);
return ms.ToArray();
}
}
C# Image to Byte Array and Byte Array to Image Converter Class
For Converting an Image object to byte[] you can do as follows:
public static byte[] converterDemo(Image x)
{
ImageConverter _imageConverter = new ImageConverter();
byte[] xByte = (byte[])_imageConverter.ConvertTo(x, typeof(byte[]));
return xByte;
}
Another way to get Byte array from image path is
byte[] imgdata = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(path));
Here's what I'm currently using. Some of the other techniques I've tried have been non-optimal because they changed the bit depth of the pixels (24-bit vs. 32-bit) or ignored the image's resolution (dpi).
// ImageConverter object used to convert byte arrays containing JPEG or PNG file images into
// Bitmap objects. This is static and only gets instantiated once.
private static readonly ImageConverter _imageConverter = new ImageConverter();
Image to byte array:
/// <summary>
/// Method to "convert" an Image object into a byte array, formatted in PNG file format, which
/// provides lossless compression. This can be used together with the GetImageFromByteArray()
/// method to provide a kind of serialization / deserialization.
/// </summary>
/// <param name="theImage">Image object, must be convertable to PNG format</param>
/// <returns>byte array image of a PNG file containing the image</returns>
public static byte[] CopyImageToByteArray(Image theImage)
{
using (MemoryStream memoryStream = new MemoryStream())
{
theImage.Save(memoryStream, ImageFormat.Png);
return memoryStream.ToArray();
}
}
Byte array to Image:
/// <summary>
/// Method that uses the ImageConverter object in .Net Framework to convert a byte array,
/// presumably containing a JPEG or PNG file image, into a Bitmap object, which can also be
/// used as an Image object.
/// </summary>
/// <param name="byteArray">byte array containing JPEG or PNG file image or similar</param>
/// <returns>Bitmap object if it works, else exception is thrown</returns>
public static Bitmap GetImageFromByteArray(byte[] byteArray)
{
Bitmap bm = (Bitmap)_imageConverter.ConvertFrom(byteArray);
if (bm != null && (bm.HorizontalResolution != (int)bm.HorizontalResolution ||
bm.VerticalResolution != (int)bm.VerticalResolution))
{
// Correct a strange glitch that has been observed in the test program when converting
// from a PNG file image created by CopyImageToByteArray() - the dpi value "drifts"
// slightly away from the nominal integer value
bm.SetResolution((int)(bm.HorizontalResolution + 0.5f),
(int)(bm.VerticalResolution + 0.5f));
}
return bm;
}
Edit: To get the Image from a jpg or png file you should read the file into a byte array using File.ReadAllBytes():
Bitmap newBitmap = GetImageFromByteArray(File.ReadAllBytes(fileName));
This avoids problems related to Bitmap wanting its source stream to be kept open, and some suggested workarounds to that problem that result in the source file being kept locked.
try this:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
You can use File.ReadAllBytes() method to read any file into byte array. To write byte array into file, just use File.WriteAllBytes() method.
Hope this helps.
You can find more information and sample code here.
If you don't reference the imageBytes to carry bytes in the stream, the method won't return anything. Make sure you reference imageBytes = m.ToArray();
public static byte[] SerializeImage() {
MemoryStream m;
string PicPath = pathToImage";
byte[] imageBytes;
using (Image image = Image.FromFile(PicPath)) {
using ( m = new MemoryStream()) {
image.Save(m, image.RawFormat);
imageBytes = new byte[m.Length];
//Very Important
imageBytes = m.ToArray();
}//end using
}//end using
return imageBytes;
}//SerializeImage
Do you only want the pixels or the whole image (including headers) as an byte array?
For pixels: Use the CopyPixels method on Bitmap. Something like:
var bitmap = new BitmapImage(uri);
//Pixel array
byte[] pixels = new byte[width * height * 4]; //account for stride if necessary and whether the image is 32 bit, 16 bit etc.
bitmap.CopyPixels(..size, pixels, fullStride, 0);
Code:
using System.IO;
byte[] img = File.ReadAllBytes(openFileDialog1.FileName);
This is Code for converting the image of any type(for example PNG, JPG, JPEG) to byte array
public static byte[] imageConversion(string imageName){
//Initialize a file stream to read the image file
FileStream fs = new FileStream(imageName, FileMode.Open, FileAccess.Read);
//Initialize a byte array with size of stream
byte[] imgByteArr = new byte[fs.Length];
//Read data from the file stream and put into the byte array
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
//Close a file stream
fs.Close();
return imageByteArr
}
To be convert the image to byte array.The code is give below.
public byte[] ImageToByteArray(System.Drawing.Image images)
{
using (var _memorystream = new MemoryStream())
{
images.Save(_memorystream ,images.RawFormat);
return _memorystream .ToArray();
}
}
To be convert the Byte array to Image.The code is given below.The code is handle A Generic error occurred in GDI+ in Image Save.
public void SaveImage(string base64String, string filepath)
{
// image convert to base64string is base64String
//File path is which path to save the image.
var bytess = Convert.FromBase64String(base64String);
using (var imageFile = new FileStream(filepath, FileMode.Create))
{
imageFile.Write(bytess, 0, bytess.Length);
imageFile.Flush();
}
}
This code retrieves first 100 rows from table in SQLSERVER 2012 and saves a picture per row as a file on local disk
public void SavePicture()
{
SqlConnection con = new SqlConnection("Data Source=localhost;Integrated security=true;database=databasename");
SqlDataAdapter da = new SqlDataAdapter("select top 100 [Name] ,[Picture] From tablename", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("tablename");
byte[] MyData = new byte[0];
da.Fill(ds, "tablename");
DataTable table = ds.Tables["tablename"];
for (int i = 0; i < table.Rows.Count;i++ )
{
DataRow myRow;
myRow = ds.Tables["tablename"].Rows[i];
MyData = (byte[])myRow["Picture"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream(#"C:\NewFolder\" + myRow["Name"].ToString() + ".jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close();
}
}
please note: Directory with NewFolder name should exist in C:\
I have response stream from a ftp web request that returns binary file.
I wanted to get the binary data into byte[] and then encode that array and send it as web service response.
Stream responseStream = webResponse.GetResponseStream();
byte[] byteToEncode= ReadFully(responseStream,1024);
String str=Convert.ToBase64String(byteToEncode);
I used that function I found online to convert the stream into byte[]
/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
/// <param name="initialLength">The initial buffer length</param>
public static byte[] ReadFully (Stream stream, int initialLength)
{
// If we've been passed an unhelpful initial length, just use 32K.
if (initialLength < 1)
{
initialLength = 32768;
}
byte[] buffer = new byte[initialLength];
int read=0;
int chunk;
while ( (chunk = stream.Read(buffer, read, buffer.Length-read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte==-1)
{
return buffer;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length*2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read]=(byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
But I got an exception says:
This stream does not support seek operations.
I would appreciate any help.
Thanks,
Sarah
You'll probably need to re-write the ReadFully function to avoid properties like Length, which need to know more about the stream than is possible when the full stream hasn't been completely received.