我正在嘗試將我的資料保存到 sqlite 并且我的資料也有照片。我想保存帶照片或不帶照片的資料。當我添加照片時,我可以將它保存到 sqlite。但是當 imagebox.Source 為空時,它會給出錯誤:
“由于物件的當前狀態,操作無效。”
它顯示錯誤:
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();
}
}
這是我的代碼:
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);
}
在助手類中,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;
}
uj5u.com熱心網友回復:
默認情況下,最大大小限制為 1000,如果您要發布要反序列化的大資料,則需要增加它。
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="2000" />
</appSettings>
將此屬性設定為過大的數字可能會帶來安全風險。
uj5u.com熱心網友回復:
由于您的資料庫將接受空值,您可能需要重構:
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;
}
附加資訊。影像轉換助手
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;
}
}
uj5u.com熱心網友回復:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="2000" />
</appSettings>
這適用于我的其他代碼。但它仍然不適用于我上面共享的代碼。
它適用于:
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);
}
它不適用于:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342705.html
上一篇:如何列印樹的所有節點數
