I am trying to save my datas to sqlite also my datas have photo. I want to save the datas with photo or without photo. when I add photo, I can save it to sqlite. but when imagebox.Source is null, it gives error :
'Operation is not valid due to the current state of the object.'
and it shows error in :
public byte[] BitmapSourceToByteArray(BitmapSource image)
{
using (var stream = new MemoryStream())
{
var encoder = new PngBitmapEncoder(); // or some other encoder
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
return stream.ToArray();
}
}
Here is my codes:
private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{
BitmapImage image = new BitmapImage();
if (ImageBox.Source != null)
{
image.BeginInit();
image.UriSource = new Uri(filephoto.FileName);
image.EndInit();
ImageBox.Source = image;
}
helper.DataSave("CompanyData", CInfo, image);
}
In helper class, DataSave;
public bool DataSave(string TableName, CustomerInfo CInfox, BitmapImage obj) // Save to DataBase
{
//try
//{
ConOpen();
string query = $"Insert Into {TableName} (Company, Product, Product_No, Product_Size, Photo) Values (@company, @product, @pno, @psize, @photo)";
SQLiteCommand komut = new SQLiteCommand(query, connection);
SQLiteParameter param1 = new SQLiteParameter("@company", DbType.String);
SQLiteParameter param2 = new SQLiteParameter("@product", DbType.String);
SQLiteParameter param3 = new SQLiteParameter("@pno", DbType.String);
SQLiteParameter param4 = new SQLiteParameter("@psize", DbType.String);
SQLiteParameter param5 = new SQLiteParameter("@photo", DbType.Binary);
param1.Value = CInfox.Customer;
param2.Value = CInfox.Product;
param3.Value = CInfox.ProductNo;
param4.Value = CInfox.ProductSize;
param5.Value = BitmapSourceToByteArray(obj);
komut.Parameters.Add(param1);
komut.Parameters.Add(param2);
komut.Parameters.Add(param3);
komut.Parameters.Add(param4);
komut.Parameters.Add(param5);
komut.ExecuteNonQuery();
MessageBox.Show("Saved Succesfully!");
ConClose();
return true;
}
CodePudding user response:
There is limit on the max size that is 1000 by default, you need to increase it if you are posting large data which is to be desrialized.
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="2000" />
</appSettings>
Setting this attribute to too large a number can pose a security risk.
CodePudding user response:
Since your database will accept null values, you may want to refactor:
public bool DataSave(string TableName, CustomerInfo CInfox, BitmapImage obj) // Save to DataBase
{
try
{
ConOpen();
string query = $"Insert Into {TableName} (Company, Product, Product_No, Product_Size) Values (@company, @product, @pno, @psize)";
SQLiteCommand komut = new SQLiteCommand(query, connection);
SQLiteParameter param1 = new SQLiteParameter("@company", DbType.String);
SQLiteParameter param2 = new SQLiteParameter("@product", DbType.String);
SQLiteParameter param3 = new SQLiteParameter("@pno", DbType.String);
SQLiteParameter param4 = new SQLiteParameter("@psize", DbType.String);
param1.Value = CInfox.Customer;
param2.Value = CInfox.Product;
param3.Value = CInfox.ProductNo;
param4.Value = CInfox.ProductSize;
komut.Parameters.Add(param1);
komut.Parameters.Add(param2);
komut.Parameters.Add(param3);
komut.Parameters.Add(param4);
if(obj is not null) // if(obj != null) for older C# versions
{
string query = $"Insert Into {TableName} (Company, Product, Product_No, Product_Size, Photo) Values (@company, @product, @pno, @psize, @photo)";
SQLiteParameter param5 = new SQLiteParameter("@photo", DbType.Binary);
param5.Value = BitmapSourceToByteArray(obj);
komut.Parameters.Add(param5);
}
komut.ExecuteNonQuery();
MessageBox.Show("Saved Succesfully!");
}
catch(Exception)
{
// Handle exceptions here or let it bubble up to caller (You should use transactions)
}
finally
{
ConClose();
}
return true;
}
Additional info. Helpers for image conversion
internal static class SignatureImageConversion
{
public static BitmapImage ConvertFileToBitmap(this string path)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(path);
image.EndInit();
return image;
}
public static BitmapImage ConvertFileStreamToBitmap(this byte[] stream)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(stream);
image.EndInit();
return image;
}
public static byte[] ConvertImageToByteArray(this BitmapImage image)
{
byte[] data = null;
JpegBitmapEncoder jpencoder = new JpegBitmapEncoder();
jpencoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
jpencoder.Save(ms);
data = ms.ToArray();
}
return data;
}
}
CodePudding user response:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="2000" />
</appSettings>
This worked for my other code. But it is not still working for my code that I shared above.
It worked for:
private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{ BitmapImage image = null;
if (ImageBox.Source != null)
{
image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(filephoto.FileName);
image.EndInit();
ImageBox.Source = image;
}
helper.DataSave("CompanyData", CInfo, image);
}
It didnt work for:
BitmapImage image = new BitmapImage();
if (ImageBox.Source != null)
{
image.BeginInit();
image.UriSource = new Uri(filephoto.FileName);
image.EndInit();
ImageBox.Source = image;
}
helper.DataSave("CompanyData", CInfo, image);