i must convert my uploaded images in base 64 to and from a sql table... my codes are as follow:
protected void ddlImage_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = ddlImage.SelectedItem.Value;
ShowImages(txtEmail.Text);
}
private void ShowImages(string email)
{
string[] images = Directory.GetFiles(Server.MapPath("~/images/customers/" + email));
ArrayList imageList = new ArrayList();
foreach (string image in images)
{
string imageName = image.Substring(image.LastIndexOf(@"\") + 1);
imageList.Add(imageName);
}
ddlImage.DataSource = imageList;
ddlImage.DataBind();
}
protected void btnuploadimage_Click(object sender, EventArgs e)
{
try
{
string folder = Server.MapPath("~/images/customers/" + txtEmail.Text);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/images/customers/" + txtEmail.Text + "/" + filename));
lblImage.Text = "image " + FileUpload1.FileName.ToString() + " successfully uploaded!";
Image1.ImageUrl = "~/images/customers/" + txtEmail.Text + "/" + filename;
}
catch (Exception)
{
lblImage.Text = "uppload Failed!";
}
}
how i should put the encoded image to sql table and how i should decode it from sql table and put in Image1.ImageUrl??!!
appreciate your respond, more grateful if you give me the codes. kind regards
venus najadPosted Nov 12, 2023, 7:12 PM
hello tuhin... i solved the problem by using:
Image1.ImageUrl = "data:image/jpeg;base64," + base64Data;
because i have downloaded base64 tools in the poroject it does decoding of the image automtically...
i have another issue and i hope you can be helpful:
in my webapplication, i fill a page dynamically by using a table and controls... it works very perfect, but when i will sort the table (using gridview), it works but the result is appended to the page... means that it fills in the end of the first page (it appends in the page)... i have tried with conditions, withut any result... how should i fix it? i tried to make the page empty and refill, without any result... i used even
mytable.Dispose();
panelAd.Controls.Clear();
panelAd.Controls.Remove(mytable);
without any result... can you solve this problem for me please? very kind regards
Tuhin PaulPosted Nov 11, 2023, 6:46 PM
In your SQL table, you should define a column with an appropriate data type for storing base64-encoded images. Usually, a data type like VARCHAR(MAX) or TEXT is used to store large text data. When saving an image, check that the image data is correctly converted to a base64 string. It's essential to verify that the image is correctly read and encoded.
When retrieving images, you should decode the base64 string back into binary data using Convert.FromBase64String. check that you retrieve the correct data from your SQL table.
venus najadPosted Nov 10, 2023, 5:24 PM
hello tuhin and thansks for your respond... i am not sure about the step 1... i will put the base64, encoded images in sql table... and then decode them from the sql table in other pages... i have a webapplication in internet, like Ebay website...
people put their advertizes with images via internet... the Ebay owner makes or updates the Ebay project in his computer and when they publish, everything is there...
but in my case, when i update my project, the images, people have put, is not in my image file on computer.... therefore, when i do changes and update the project, the image file cannot find those images and it is empty in their posts.. therefore i thought i can use base 64... save encoded image in sql table and decode when we read from the table...
the codes i have used are:
Image1.ImageUrl = "data:image/jpeg;base64," + Base64StringOfImageStoredInDatabase;
(but Base64StringOfImageStoredInDatabase is a feild, which its dafault value is null... how should i define it? thanks)
var img = System.Drawing.Image.FromStream(FileUpload1.FileContent); // Throws error if no correct image
using (var ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // make sure we only have JPG
string base64Data = Convert.ToBase64String(ms.ToArray());
// NOW SAVE base64String to database for further use
Image1.ImageUrl = base64Data; (but it is empty or null in the table??!! it seemjs that convertion doesnt work)
}
for decoding i have used:
var decodedimage=Convert.FromBase64String(reader.GetString(9)).ToString(); (but it is empty, maybe conversion doesnt work properly) i have downloade base64 encode/decode tools too, without any changes!!!?
i appreciate if you correct my codes and give me the codes... otherwise it is difficult for me to understande and write codes. thanks and kind regards
Tuhin PaulPosted Nov 10, 2023, 3:23 PM
Storing images as base64-encoded strings in a SQL table can be inefficient for large images. Check the steps to store images in a SQL table as base64-encoded strings and retrieve them for display
Step 1: Create a SQL table with a column to store the base64-encoded image data, along with other necessary columns for identification and metadata.
Step 2: After saving the image in your server directory, encode it to base64 and insert it into the SQL table. Use File.ReadAllBytes to read the image file and Convert.ToBase64String to encode it.
Step 3: To display the image, you can retrieve the base64-encoded string from the SQL table and decode it. Use the Convert.FromBase64String method to decode the base64 string back into a byte array and then display it.
Step 4: Set the ImageUrl property of the Image control to the base64-encoded image preceded by the appropriate data URL scheme