none
How to convert image to byte array ?

    General discussion

  • Hi, 

    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 ??
    Friday, April 27, 2012 11:00 AM

All replies

  • cranberries_,

    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

    Patrick

    Friday, April 27, 2012 11:36 AM
  • Patrick ,

    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.

    Saturday, April 28, 2012 2:20 AM
  • Canberries_,

    but you have the byte[] version of the image, after the call to Read(...)!

    In your previous message, you allocate the buffer, but do not read the image into it.

    Patrick

    Saturday, April 28, 2012 2:32 AM
  •  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 ?

    Saturday, April 28, 2012 3:54 AM
  • _cranberries,

    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);

    Patrick

    Saturday, April 28, 2012 6:50 AM
  •   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..
    Sunday, April 29, 2012 3:57 PM
  • Cranberries,

    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.

    Patrick

    Monday, April 30, 2012 12:46 AM
  • Patrick,

    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.

    Monday, April 30, 2012 9:45 AM
  • cranberries,

    can you please explain what you really want to do?

    it will take less time if you tell me the whole story, instead of given it piece after piece?

    Patrick

    Tuesday, May 01, 2012 2:12 AM
  • Patrick ,

    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.

    Wednesday, May 02, 2012 1:47 AM
  • cranberries,

    From where do the image come?
    - does the image belongs to the application?
    - it is a user defined image?

    Is this a in-browser or out-of-browser application?

    In case of in-browser application, is the application a trusted one?

    Patrick

    Thursday, May 03, 2012 12:39 PM
  • Patrick,

    The image is located in a folder at client side in the project .  And the project is a in - browser application . Yes it is a trusted application .

    Thanks..

    Friday, May 04, 2012 6:13 AM
  • Cranberries,

    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.

    Patrick

    Friday, May 04, 2012 11:51 AM