1

I need help to get the string from a blob that has a Base64string that is uploaded and the image details saved in an mySql database by a client using a third party software. Here is the code i use to get the byte from the mysql database

byte[] imageByte =( byte[]) (dtImages.Rows[0]["fldData"]);

this is how i try to read it get into a string so that i can convert it back. It will not allow me to perform this conversion as it is a byte.

string image1Str = Convert.FromBase64String(imageByte);

How can i read the string from this to perform the conversion, once i have the string i can then save it as an image.

0

2 Answers 2

1

Please try the following code.

Just replace the connection with your data connection, and replace <your query here> with the query to the BLOB object:

using System;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.IO;

.... 

FileStream fs;                          // Writes the BLOB to a file.
BinaryWriter bw;                        // Streams the BLOB to the FileStream object.
int bufferSize = 100;                   // Size of the BLOB buffer.
byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.
long retval;                            // The bytes returned from GetBytes.
long startIndex = 0;                    // The starting position in the BLOB output.

SqlDataReader myReader = connection.ExecuteReader(<your query here>);

while (myReader.Read())
{
    fs = new FileStream("my.bmp", FileMode.OpenOrCreate, FileAccess.Write);
    bw = new BinaryWriter(fs);
    startIndex = 0;
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

    while (retval == bufferSize)
    {
         bw.Write(outbyte);
         bw.Flush();
         startIndex += bufferSize;
         retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
    }

    bw.Write(outbyte, 0, (int)retval);
    bw.Flush();
    bw.Close();
    fs.Close();
}
myReader.Close();
connection.Close();
Sign up to request clarification or add additional context in comments.

13 Comments

Thank-you for your timely reply., but when i try to put this code byte[] bytes = Convert.FromBase64String(dtImages.Rows[0]["fldData"]); getting a syntax error... cannot convert object to string
What are the values in your database? Can you post an example of one of the values in dtImages.Rows[0]["fldData"] ?
the error is a syntax error... the blob is huge..unable to paste it
What is the column fldData data type in the database? Blob ?
Koby, trying it...will let you know shortly.
|
0

Just my 2 cents, I think you should use Convert.ToBase64String instead of Convert.FromBase64String to get what you need.

More info at MSDN about

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.