none
C# How to get image from array of bytes (blob converted into array of bytes)? RRS feed

  • Question

  • We are saving a picture from android app to MySQL blob. Now we want to get that image in C#. Can anyone suggest a equivalent code for C#? We want to show original image. I have successfully retrieved data from database. it is returning me array of bytes.I have tried lots of methods but I am getting image with small size and with low quality

    public String getStringImage(Bitmap bmp){
       ByteArrayOutputStream baos=new ByteArrayOutputStream();
       bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
       byte[] imageBytes= baos.toByteArray();
       String encodedImage= Base64.encodeToString(imageBytes, Base64.DEFAULT)
       return encodedImage;

    }



    • Edited by Ajay0311 Monday, November 6, 2017 1:41 PM
    Monday, November 6, 2017 12:56 PM

All replies

  • If it is an image then you can use Image to convert the byte array to the displayable image, for Winforms. If you just have the byte array then put it into a MemoryStream and then use FromStream to read it. If it is on disk already then use FromFile instead. You should read the documentation on what image formats the type supports. It is limited. If it doesn't support the format you need then you'll have to use a third party library.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 6, 2017 3:05 PM
    Moderator
  • Hello Ajay0311,

    For how to convert byte array to image, try the below code. It works well as long as you didn't miss any data when you store image to database.

                //SqlConnection conn = new SqlConnection("Data Source=xxxx;Initial Catalog=xxxx;Integrated Security=xxxx");
                //conn.Open();
                //SqlCommand cmd = new SqlCommand("xxxxxx", conn);
                //SqlDataReader reader = cmd.ExecuteReader();
                //reader.Read();
                byte[] imageBytes =(byte[]) reader[0];
                MemoryStream buf = new MemoryStream(imageBytes);
                Image image = Image.FromStream(buf, true);
                image.Save("D:\\Test\\test.jpg");

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 7, 2017 6:18 AM
    Moderator
  • Hello Ajay0311,

    Is there anything updated?If the issues has been solved, you could close the thread by marking  as answer, This can be beneficial to other community members reading this thread.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 10:49 AM
    Moderator
  • Please Can you send me ur code that how u retrieve the image from Mysql Blob because i can't retrieve it with the same scenario but i want to retrieve in ASP.NET MVC5. Please Help me..

    Thank You:)


    Friday, March 1, 2019 5:16 AM