# Agregar imagen a blob (mysql) a través de c #

I have made a C# application that adds images from the harddisk to a blob-field in mysql. I don't get errors and the blob-field is filled.

When I check the blob-field in MySQL Workbench it says the field is not correct (it can not show the image).

Whatever I do, whatever example I try, nothing seems to work. So it has no point to place any example code here, because nothing is working so far.

Does anyone have a good example code that I can try?

Muchas Gracias

preguntado el 01 de septiembre de 12 a las 16:09

The code from your C# app might be useful; if for no reason but to verify that what you're doing is correct. -

What kind of image is it? Maybe MySQL Workbench doesn't know how to show a TIF (for example). Try grabbing the bytes and seeing if you CAN view the view image. -

@Cory: Some link is posted with code. I will check it, try it and posted here. -

@aquinas: Nothing special, just a png 16x16. When I add the image directly with Workbench, it is showing correctly. -

OK. Then POST YOUR CODE. Write a 5 line console application that can replicate the problem. Then we can help you. It's highly doubtful that MySQL has a problem. :) -

## 3 Respuestas

Okay, since some people wanted some code to be shown, I created a very simple console application with the help described in the link of Alex. I literally taken that code and placed in the console app. Some miracle happend, because it worked as soon as I started the app. I don't know what I did wrong in the main application, but it has to be something (duh). Here is the code:

string MyConString = "SERVER=localhost;" +
"DATABASE=database;" +
"UID=root;" +

System.IO.FileStream fs = new FileStream(@"D:\link\to\image.png", FileMode.Open);
System.IO.BufferedStream bf = new BufferedStream(fs);
byte[] buffer = new byte[bf.Length];

byte[] buffer_new = buffer;

MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = new MySqlCommand("", connection);
command.CommandText = "insert into table(fldImage) values(@image);";

command.ExecuteNonQuery();

connection.Close();



Respondido el 02 de Septiembre de 12 a las 13:09

Be careful in here. You never close the fs FileStream or the bf buffered stream. Your main problem could be that buffer_new no contiene todos los of the bytes of you image, resulting in a corrupted one in MySQL Workbench. You really should be applying the using statement to streams: using (var fs = new FileStream(@"D:\link\to\image.png", FileMode.Open)) { using (var bf = new BufferedStream(fs)) { /* your existing code */ } } - Cᴏʀʏ

Did you try to recover the blob via the mysql connector (the way you store it). MySQL Workbench doesn't displaying the image doesn't mean that isn't being stored.

Try to recover the image:

while (reader.read())
{
File.WriteAllBytes(@"c:\image.extension", image);
}


This way you can ensure it's saving.

Respondido el 01 de Septiembre de 12 a las 16:09

Tried your code, image is saved, image can not be shown by windows image viewer. So... Now I can say the image is saved incorrect? - user1140479

If you try with the proper extension then yes. It's not being saved the way it should. I'll suggest you to put the code you're using to see where's the problem. - Erre Efe

I find when working with mysql and .NET that the Conector MySQL makes things really easy!

Este chico: Guardar imagen en MySQL a través de la aplicación C # seems to be on the right lines.

¡Buena suerte!

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.