c# - 在 WPF 中 ImageBox.Source == null 时无法保存 SQLİte
问题描述
我正在尝试将我的数据保存到 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;
}
解决方案
由于您的数据库将接受空值,您可能需要重构:
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;
}
}
推荐阅读
- php - PHP 致命错误:未捕获的错误:函数名必须是字符串
- django - 通过模板执行视图后,Django 视图无法正确呈现
- r - R to MySQL 抛出错误“无法运行语句:无效的 utf8mb4 字符串”
- c# - 不等待 Async 方法,该方法又在其中等待
- javascript - 滑块菜单使外部元素不可点击
- c++ - 在 cmake3 上指定临时构建文件路径
- azure - 导出所有 Azure AD 组及其成员 (PowerShell)
- json - VBA JsonConverter 添加额外的引号
- python - Matplotlib - 让颜色条与绘图中的颜色对齐并使用原始值作为标签
- r - 低于阈值的值的百分比(按年份)