3

How can i convert binary data stored in my database field to Byte[] array?

simply casting binary as byte[] is not working

context.Response.BinaryWrite((byte[])images);
5
  • 7
    What's the exact type of images? Commented Jul 5, 2011 at 12:59
  • Are you getting the binary string? Commented Jul 5, 2011 at 13:01
  • What is the data type of the database object? Commented Jul 5, 2011 at 13:03
  • datatype of image VarBinary(Max) Commented Jul 5, 2011 at 13:05
  • 2
    VarBinary(max) is no C# data type... Commented Jul 5, 2011 at 13:08

2 Answers 2

5

If images is a single record of type Binary then calling toArray should work

 context.Response.BinaryWrite(images.toArray());
Sign up to request clarification or add additional context in comments.

Comments

2
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.