distal-attribute
distal-attribute
distal-attribute
distal-attribute

MySQL LONGBLOB to JPG

sjlink posted 10 months ago in General
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 posted 10 months ago
Support C# code here? No clue. Especially as you haven't posted any code here.
sjlink posted 10 months ago
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 posted 10 months ago
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!
sjlink posted 10 months ago
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.