public delegate void DownloadPhotoCompleted(byte[] imageBytes);
public event DownloadPhotoCompleted DownloadPhotoCompletedEvent;
void worker_DoWork(object sender, DoWorkEventArgs e)
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(uri);
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
int length = (int)e.Result.Length;
BinaryReader br = new BinaryReader(e.Result);
byte[] buffer = new byte[length];
using (br)
{
for (int i = 0; i < length; i++)
{
buffer[i] = br.ReadByte();
}
}
if (DownloadPhotoCompletedEvent != null)
DownloadPhotoCompletedEvent(buffer);
}
Note that the external site needs to have a crossdomain.xml or clientaccesspolicy.xml defined to allow access by your application. If not you will encounter a System.SecurityException.
Showing posts with label byte[]. Show all posts
Showing posts with label byte[]. Show all posts
Thursday, August 19, 2010
Wednesday, August 19, 2009
C#: Read a file into a byte array
public byte[] readByteArrayFromFile(string fileName){
byte[] buff = null;
try{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int)numBytes);
}
catch (Exception ex) {Console.WriteLine(ex.Message); }
return buff;
}
Another option:
byte[] rawData = File.ReadAllBytes("filename");
(This method sacrifices the ability to have more explicit control on the file like FileAccess.Read etc.)
Source: http://kseesharp.blogspot.com/2007/12/read-file-into-byte-array.html
Subscribe to:
Posts (Atom)