First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
I use the following code to insert the image and retrieve it from the Access database, and I have no problem retrieving it after inserting the image.
BitmapImage BM;
private void UploadButton_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
System.Windows.Forms.OpenFileDialog OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
OpenFileDialog.AutoUpgradeEnabled = false;
if (App.EnumLanguage.Equals(AllLanguage.English))
{
OpenFileDialog.Title = "Selecting Image";
}
else
{
OpenFileDialog.Title = "انتخاب تصویر";
}
OpenFileDialog.Filter = "JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp|GIF(*.gif)|*.gif|PNG(*.png)|*.png|All Files|*.*";
if (OpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var IMG = System.Drawing.Image.FromFile(OpenFileDialog.FileName);
BM = new BitmapImage(new Uri(OpenFileDialog.FileName));
BitmapImage BitMapImage = new BitmapImage();
BitMapImage.BeginInit();
System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream();
IMG.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
MemoryStream.Seek(0, System.IO.SeekOrigin.Begin);
BitMapImage.StreamSource = MemoryStream;
BitMapImage.EndInit();
BookImage.Source = BitMapImage;
OpenFileDialog.Dispose();
}
}
private static byte[] ImageToBytes(BitmapImage image)
{
byte[] Data;
JpegBitmapEncoder JpegEncoder = new JpegBitmapEncoder();
JpegEncoder.Frames.Add(BitmapFrame.Create(image));
using (System.IO.MemoryStream MS = new System.IO.MemoryStream())
{
JpegEncoder.Save(MS);
Data = MS.ToArray();
}
return Data;
}
private BitmapImage GetImageFromBytes(byte[] bytes)
{
System.IO.MemoryStream Stream = new System.IO.MemoryStream();
Stream.Write(bytes, 0, bytes.Length);
Stream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(Stream);
BitmapImage bitImage = new BitmapImage();
bitImage.BeginInit();
System.IO.MemoryStream MS = new System.IO.MemoryStream();
img.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);
MS.Seek(0, System.IO.SeekOrigin.Begin);
bitImage.StreamSource = MS;
bitImage.EndInit();
return bitImage;
}
private void Add_Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
OleDbConnect.Open();
if (BM != null)
{
byte[] Image_Bytes = ImageToBytes(BM);
OleDbParameter Parameter = new OleDbParameter();
Parameter.OleDbType = OleDbType.Binary;
Parameter.ParameterName = "Image";
Parameter.Value = Image_Bytes;
OleDbCommand OleDbCommand_Insert = new OleDbCommand("Insert Into [BookTable](BookName,Publisher,Category,IDNumber,Status,HistoryTaken,RecipientName,ReturnDate,BookImage)values('" + BookName_TextBox.Text + "','" + Publisher_TextBox.Text + "','" + Category_ComboBox.Text + "','" + IDNumber_TextBox.Text + "','" + Status_ComboBox.Text + "','" + HistoryTaken_TextBox.Text + "','" + RecipientName_TextBox.Text + "','" + ReturnDate_TextBox.Text + "',@Image)", OleDbConnect);
OleDbCommand_Insert.Parameters.Add(Parameter);
OleDbCommand_Insert.ExecuteScalar();
}
OleDbConnect.Close();
}
\\\ retrieving information from Access Database
OleDbCommand OleDCmd = new OleDbCommand("Select * From BookTable Where IDNumber='" + Search_ComboBox.Text.Trim() + "'", OleDbConnect);
OleDCmd.CommandType = System.Data.CommandType.Text;
OleDbConnect.Open();
OleDbDataReader DataReader = OleDCmd.ExecuteReader();
while (DataReader.Read())
{
BookName_TextBox.Text = DataReader[0].ToString();
Publisher_TextBox.Text = DataReader[1].ToString();
Category_ComboBox.Text = DataReader[2].ToString();
IDNumber_TextBox.Text = DataReader[3].ToString();
Status_ComboBox.Text = DataReader[4].ToString();
HistoryTaken_TextBox.Text = DataReader[5].ToString();
RecipientName_TextBox.Text = DataReader[6].ToString();
ReturnDate_TextBox.Text = DataReader[7].ToString();
BitmapImage BMP = GetImageFromBytes((byte[])DataReader[8]);
BookImage.Source = BMP;
}
But when I use the following commands to update the image and other information, this error is displayed when I want to retrieve the information.
byte[] Image_Bytes = ImageToBytes(BM);
OleDbParameter Parameter = new OleDbParameter();
Parameter.OleDbType = OleDbType.Binary;
Parameter.ParameterName = "Image";
Parameter.Value = Image_Bytes;
OleDbCommand OleDbCommand_Update = new OleDbCommand("Update [BookTable] Set BookName='"+BookName_TextBox.Text.Trim()+"',Publisher='"+Publisher_TextBox.Text.Trim()+"',Category='"+Category_ComboBox.Text.Trim()+"',Status='"+Status_ComboBox.Text.Trim()+"',HistoryTaken='"+HistoryTaken_TextBox.Text.Trim()+"',RecipientName='"+RecipientName_TextBox.Text.Trim()+"',ReturnDate='"+ReturnDate_TextBox.Text.Trim() +"',BookImage='"+ (BitmapImage)BookImage.Source + "'Where IDNumber='" + IDNumber_TextBox.Text.Trim()+ "'", OleDbConnect);
OleDbCommand_Update.Parameters.Add(Parameter);
OleDbCommand_Update.ExecuteScalar();
System.ArgumentException: 'Parameter is not valid.' Line 6 gives an error
private BitmapImage GetImageFromBytes(byte[] bytes)
{
System.IO.MemoryStream Stream = new System.IO.MemoryStream();
Stream.Write(bytes, 0, bytes.Length);
Stream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(Stream);\\System.ArgumentException: 'Parameter is not valid.'
BitmapImage bitImage = new BitmapImage();
bitImage.BeginInit();
System.IO.MemoryStream MS = new System.IO.MemoryStream();
img.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);
MS.Seek(0, System.IO.SeekOrigin.Begin);
bitImage.StreamSource = MS;
bitImage.EndInit();
return bitImage;
}
In short: in insert command i have no problem to retrieve image but in update command i have problem to retrieve image.
In my opinion, the Update command does not save the image properly in the Access database.
Thanks
What I have tried:
byte[] Image_Bytes = ImageToBytes(BM);
OleDbParameter Parameter = new OleDbParameter();
Parameter.OleDbType = OleDbType.Binary;
Parameter.ParameterName = "Image";
Parameter.Value = Image_Bytes;
OleDbCommand OleDbCommand_Update = new OleDbCommand("Update [BookTable] Set BookName='"+BookName_TextBox.Text.Trim()+"',Publisher='"+Publisher_TextBox.Text.Trim()+"',Category='"+Category_ComboBox.Text.Trim()+"',Status='"+Status_ComboBox.Text.Trim()+"',HistoryTaken='"+HistoryTaken_TextBox.Text.Trim()+"',RecipientName='"+RecipientName_TextBox.Text.Trim()+"',ReturnDate='"+ReturnDate_TextBox.Text.Trim() +"',BookImage='"+ (BitmapImage)BookImage.Source + "'Where IDNumber='" + IDNumber_TextBox.Text.Trim()+ "'", OleDbConnect);
OleDbCommand_Update.Parameters.Add(Parameter);
OleDbCommand_Update.ExecuteScalar();