MySQL LONGBLOB to JPG

[expired user #7799]'s profile image [expired user #7799] posted 10 years ago in General Permalink
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?
ansgar's profile image ansgar posted 10 years ago Permalink
Support C# code here? No clue. Especially as you haven't posted any code here.
[expired user #7799]'s profile image [expired user #7799] posted 10 years ago Permalink
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");
kalvaro's profile image kalvaro posted 10 years ago Permalink
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!
[expired user #7799]'s profile image [expired user #7799] posted 10 years ago Permalink
I think I may finally get it. Read in with binary reader and then write out with binary writer. I will try that. Is there a better place to post this question?

Please login to leave a reply, or register at first.