I am using two ways:
private void button_Upload_Click(object sender, EventArgs e)
{
string EventNumber = comboBox_EventNumber.Text;
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "png Files(*.png)|*.png|jpg Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
if(dialog.ShowDialog() == DialogResult.OK)
{
imgLocation = dialog.FileName.ToString();
pictureBox1.ImageLocation = imgLocation;
Filename = dialog.FileName;
}
DialogResult result = new DialogResult();
result = MessageBox.Show("A you sure u want to save this Picture", "Saving Picture", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
byte[] images = null;
FileStream stream = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(stream);
images = br.ReadBytes((int)stream.Length);
string query = "Update Events SET FileName = '" + @Filename + "', Cover = '" + @images + "'where ID = '" + EventNumber + "'";
SqlCommand updateCommand = new SqlCommand(query);
updateCommand.Parameters.AddWithValue("@Filename", Filename);
updateCommand.Parameters.AddWithValue("@images", @images);
int row = dataBase.executeQuery(updateCommand);
if (row >= 1)
{
MessageBox.Show("Image Uploaded Successfully");
}
else
{
MessageBox.Show("Error occured");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
It works but not as I should can specific the row where I want to insert/update ,but when the data is inserted instead of getting "0xFFD8FFE000104A46494600010101000000000000FFE20240494....."(a lot of more characters) i get "0x53797374656D2E427974655B5D".
The problem is when I try to retrieve the image from the database I can not retrieve it from the second one only from the first one.
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "JPEG|*.jpg|PNG|*.PNG|ALL|*.*", ValidateNames = true, Multiselect = false })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Filename = openFileDialog.FileName;
pictureBox1.Image = Image.FromFile(Filename);
using (AmericanCornerMembersEntities1 ACME = new AmericanCornerMembersEntities1())
{
MyPic pic = new MyPic() { Filename = Filename, Data = ConvertImageToBinary(pictureBox1.Image) };
ACME.MyPics.Add(pic);
await ACME.SaveChangesAsync();
MessageBox.Show("Succusfully Saved");
Here it is saved as it should the data but I can not specific which row it should update/insert.
byte[] ConvertImageToBinary (Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
What I have tried:
What I tried to make it work is:
<pre> /* byte[] images = null;
FileStream stream = new FileStream(imgLocation, FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(stream);
images = br.ReadBytes((int)stream.Length);*/
var images = ConvertImageToBinary(pictureBox1.Image);
Still same results even with or without commenting out that part.