C
C#2mo 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
LunaOP2mo ago
btw my client.image variable is this: public Image? Image { get; set; } = image;
mg
mg2mo 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
TizzyT2mo ago
can base64 encode it if all else fails
mg
mg2mo ago
But what's happening is happening because ToString() is implicitly being called on client.Image, which returns the type name by default
Luna
LunaOP2mo ago
i'll try
mg
mg2mo ago
You should be using whatever MySQL's type for binary data is
TizzyT
TizzyT2mo ago
yup i said if all else fails store as bytes or blob, etc
Luna
LunaOP2mo ago
yes, i'm using a binary type
mg
mg2mo ago
And for smaller images it can be ok to store them in the database
Luna
LunaOP2mo ago
i can't, unfortunately also true The scope of this project, is for a local store
TizzyT
TizzyT2mo ago
then why not just move the images to a specific location and just store the path to it? is that not allowed?
Luna
LunaOP2mo ago
The problem is, if the computer breaks, the images gets lost forever
mg
mg2mo ago
That's always going to be true
TizzyT
TizzyT2mo ago
isnt that the same if the db is hosted on the same computer?
mg
mg2mo ago
What does storing them in a database solve
Luna
LunaOP2mo ago
and it was promissed to the client that everything will be have backup
TizzyT
TizzyT2mo ago
so the db is online gotcha
Luna
LunaOP2mo ago
not in this case, the db is online
mg
mg2mo ago
I didn't say store them on the client's filesystem, I said store them somewhere that's designed for storing files
Luna
LunaOP2mo ago
i know, but i can't do this i'll try to convice my boss to buy AWS for images anyways, thanks
mg
mg2mo 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
LunaOP2mo 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
TizzyT2mo ago
lol
Angius
Angius2mo 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
LunaOP2mo ago
agree
Want results from more Discord servers?
Add your server