Hi,
I have C# code that reads a LONGBLOB field from MySQL and writes that to a JPG file. The size of the resulting file is almost twice what your "export to image" option creates and it will not open in MSPaint. I am sure that it is a difference in encoding but I can't find anything helpful. Would you happen to have any suggestions?
MySQL LONGBLOB to JPG
Sorry about that, I thought you guys may have a generic suggestion based on the routine in your software to accomplish this.
List<string> linearr = new List<string> { };
string[,] classarr;
int counter = 0;
int x;
string line;
string[] splitln = new string[12];
string inscommand = "";
MySqlConnection conn = new MySqlConnection("SERVER=;Database=;UID=;PASSWORD=");
conn.Open();
Console.WriteLine("Connected to database, importing classexport.txt file");
byte[] fs;
BinaryWriter pic;
string fnm;
string inppic;
using (MySqlCommand cmd = new MySqlCommand("select Email,ImageFile from employeedirectory;", conn))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
fnm = reader.GetString(0).Substring(0,reader.GetString(0).IndexOf("@"));
if (reader.GetValue(1).ToString() != "")
{
FileStream pic2 = new FileStream(@"F:\mysql_data\OUPics\" + fnm + ".jpg", FileMode.CreateNew);
UTF8Encoding enc = new UTF8Encoding();
fs = enc.GetBytes(reader.GetString(1).ToString());
pic = new BinaryWriter(pic2);
pic.Write(fs,0,fs.Length);
Console.WriteLine(fnm);
}
}
}
}
conn.Close();
Console.WriteLine("process finished");
Putting aside the offtopicness of the subject... Difference in encoding? That's most likely the problem. Images are binary data, not text, they do not have encoding at all. If your C# code converts raw bytes to what seems to be UTF-16 then you're handling it as text. Don't!
Please login to leave a reply, or register at first.