I should read an image which type is png in my project. I have a folder named "Images". I put my image to this folder. I should read this image and convert it to buyt array. I tried this after exploring this issue a long time at net,
FileStream stream = new FileStream("/Images/Cloud.png", FileMode.Open); BinaryReader reader = new BinaryReader(stream); byte tempImage = new byte[reader.BaseStream.Length];
"tempImage" is generated but all pixels are 0. It can not read now the image. How can I read the image and convert it to byte array ?
Thanks for the replies in advance ??
your code should be:
FileStream stream = new FileStream("/Images/Cloud.png", FileMode.Open); BinaryReader reader = new BinaryReader(stream); var FileSize = reader.BaseStream.Length; // size of the file byte tempImage = new byte[FileSize]; // allocate a buffer reader.Read(tempImage, 0, FileSize); // read the file
Thanks for your reply, but I should have the byte version of my image. I will use it in my project. I mean how can I convert the image which I read to byte. I have written in my previous message but it does not seem.
Thank for the replies in advance.
I have an object which have a property named "Image" , and type of this property is byte . I should take the image which is read and set this image to my objects's Image property. But the read method returns int ?
By the way , I realized that stream.length comes 0. I think , I can not take the image with file stream. Isn't file stream an approciate solution ? And actuallay I can read and show the image as image but can not convert it to byte array ?
the first parameter of the Read() method is the array on which you read the file content.
I'm currently using the following code to read an image from a file to a byte array and it works:
Var Length := Stream.Length; Var Buffer := New Byte [Length]; Stream.Seek (0, SeekOrigin.Begin); Stream.Read (Buffer, 0, Length);
Partrick I thank you again for your reply..After research I solved the problem with :
byte image= File.ReadAllBytes(pathOfImage);
When I set an image in for path desktop, it works.
But in project I should have the project path to read the image in the project.
I tried which I find as solutions like :
string s1 = Environment.CurrentDirectory; string s2 = Path.GetDirectoryName(Application.Current.ToString()); string s3= System.IO.Directory.GetCurrentDirectory();
But all these code blocks shows the desktop "C:\Users\MyUser".
How can I take the projet directory in silverlight ?
Thanks for the replies in advance..
what do you mean by project directory?
Is it the project where the application is installed? In this case, there is no such thing in an In-Browser application. For Out-Of-Browser application, have you tried Assembly.GetExecutingAssembly.FullName?
Or do you want to just load an image that was installed with the application? In this case, just put the image as a resource in your assembly and load it using an URL.
var uri = new Uri(Application.Current.Host.Source,"..ProjectName/Images/Cloud.png"); byte photodata = File.ReadAllBytes(uri.AbsolutePath);
File.ReadAllBytes method casts uri to C:\ folder whatever the parameter is in it. "C:\ " + uri.AbsolutePath comes from function.
And it can not find the path of image ..
I tried it before but It does not work.I could not understand your second suggestion exactly. Is that possible for you to explain it in more details ?
By the way , I had an idea about why I could not take the local path of project in silverlight. The project works on client side , and it shows the client's desktop as path. I thought that I can take the image from web service, and set it . How can I take the uri in web service ? Or Is that possible to take the image from web service and read it ?
Thanks for the reply in advance.
I have an image (.png) in a folder at project(at Client side.) . There is an object which have a property like "Image". Type of this property is byte array. I just want to read this image and set this image to my object "Image " property.
As a solution , I tried to read image and convert it into byte array. But I couldn't apply it . After that I find the File.ReadAllBytes(string path) method as another solution. When I give any path for this method , it casts the path to C:\
EX: For path : MyProject\Folders\Images\MyImage.png
It converts the path to => C:\MyProject\Folders\Images\MyImage.png and could not find the image because there isn't any path like this.
1. If the image is located in a folder at client side, it means that the user has to select it, no?
So you can open the file directly from the result of the OpenFileDialog call.
2. With an in-browser application, there is no such thing as the application directory, because the application is not installed in the user machine, but lives in the browser cache.