C
C#3w ago
Luna

Save an image to a mysql

cmd = new MySqlCommand(query, con); cmd.Parameters.AddWithValue("@image", client.Image); I tried this method, but saved "System.Image.bitmap" on the database How can I save the image to the mysql database?
25 Replies
Luna
Luna3w ago
btw my client.image variable is this: public Image? Image { get; set; } = image;
mg
mg3w ago
You'll want to store images with a service that's built for storing large amounts of binary data, not in your SQL database.
TizzyT
TizzyT3w ago
can base64 encode it if all else fails
mg
mg3w ago
But what's happening is happening because ToString() is implicitly being called on client.Image, which returns the type name by default
Luna
Luna3w ago
i'll try
mg
mg3w ago
You should be using whatever MySQL's type for binary data is
TizzyT
TizzyT3w ago
yup i said if all else fails store as bytes or blob, etc
Luna
Luna3w ago
yes, i'm using a binary type
mg
mg3w ago
And for smaller images it can be ok to store them in the database
Luna
Luna3w ago
i can't, unfortunately also true The scope of this project, is for a local store
TizzyT
TizzyT3w ago
then why not just move the images to a specific location and just store the path to it? is that not allowed?
Luna
Luna3w ago
The problem is, if the computer breaks, the images gets lost forever
mg
mg3w ago
That's always going to be true
TizzyT
TizzyT3w ago
isnt that the same if the db is hosted on the same computer?
mg
mg3w ago
What does storing them in a database solve
Luna
Luna3w ago
and it was promissed to the client that everything will be have backup
TizzyT
TizzyT3w ago
so the db is online gotcha
Luna
Luna3w ago
not in this case, the db is online
mg
mg3w ago
I didn't say store them on the client's filesystem, I said store them somewhere that's designed for storing files
Luna
Luna3w ago
i know, but i can't do this i'll try to convice my boss to buy AWS for images anyways, thanks
mg
mg3w ago
Fair enough, again if the images are small there's probably not a huge problem with storing them in the database Just make sure you're storing them in their own table
Luna
Luna3w ago
worked converting to base64
public static string EncodeBase64(Image image, ImageFormat format)
{
MemoryStream memoryStream = new();

image.Save(memoryStream, format);
byte[] imageBytes = memoryStream.ToArray();

return Convert.ToBase64String(imageBytes);
}
public static string EncodeBase64(Image image, ImageFormat format)
{
MemoryStream memoryStream = new();

image.Save(memoryStream, format);
byte[] imageBytes = memoryStream.ToArray();

return Convert.ToBase64String(imageBytes);
}
TizzyT
TizzyT3w ago
lol
Angius
Angius3w ago
Just saying, but storing files in the database will be slow as shit Not only will it slow down your queries and bloat the db You'll be unable to use compression and caching
Luna
Luna2w ago
agree
Want results from more Discord servers?
Add your server