C# 如何使用dbContext WPF c在数据库sqlite中添加图像?
我想以字节形式在图像列中将图像添加到数据库中 我使用SQLite保存数据库数据,使用dbContext c编写代码保存WPF应用程序 有人能帮我吗C# 如何使用dbContext WPF c在数据库sqlite中添加图像?,c#,wpf,sqlite,dbcontext,C#,Wpf,Sqlite,Dbcontext,我想以字节形式在图像列中将图像添加到数据库中 我使用SQLite保存数据库数据,使用dbContext c编写代码保存WPF应用程序 有人能帮我吗 private void ChooseImageButtonClick(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Filter = "Choose
private void ChooseImageButtonClick(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif";
if (dlg.ShowDialog() == true)
{
string FileName = dlg.FileName.ToString();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(FileName);
bitmap.EndInit();
ImageBox.Source = bitmap;
}
}
private void savebtnClick(object sender, RoutedEventArgs e)
{
using (DatabaseContext dbContext = new DatabaseContext())
{
Person p = new Person
{
Id = int.Parse(Idtextbox.Text),
Name = Nametextbox.Text,
Image = image
};
dbContext.Person.Add(p);
dbContext.SaveChanges();
RefreshList();
}
}
只需先将BitmapImage转换为字节数组即可
byte[] image;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 100;
using (MemoryStream ms = new MemoryStream())
{
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)ImageBox.Source));
encoder.Save(ms);
image = ms.ToArray();
}
encoder = null;
在数据库上下文中,添加
using (DatabaseContext dbContext = new DatabaseContext())
{
Part part = new Part();
part.Id = int.Parse(TextBoxID.Text);
part.Name = TextBoxName.Text;
part.Image = image;
dbContext.Part.Add(part);
dbContext.SaveChanges();
RefreshPartsList();
}}
要将字节数组转换回BitmapImage,请执行以下操作:
byte[] imageData = part.Image; // that you get from db
if (imageData == null || imageData.Length == 0)
{
//Show error msg or return here;
return;
}
var image = new BitmapImage();
using (var ms = new System.IO.MemoryStream(imageData))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
image.Freeze();
}
在“Part”类中添加属性
public byte[] ImageCol { get; set; }
然后从OpenFileDialog从所选文件创建图像。比如说
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG)|*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};
if (openFileDialog.ShowDialog()==DialogResult.OK)
{
var imageFromFile = System.Drawing.Image.FromFile(openFileDialog.FileName);
part.ImageCol = imageFromFile.ConvertBitmapImagetoBytes();
}
将位图转换为字节[]
public static byte[] ConvertBitmapImagetoBytes(this Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
这不是WPF,而是WinForms。方法名ConvertBitmapImagetoByte with BitMapImages至少有误导性,如果没有错误的话。很好。但这并不是在WPF中执行的方式。也就是说,在您的示例中,创建位图是毫无意义的。您只想从文件中获取图像字节,因此part.ImageCol=file.ReadAllBytesopenFileDialog.FileName;应该足够了。好的,但在我的示例中,我进一步使用了位图对象。所以我先把它转换成位图:也许吧,但是你完全忽略了OP已经有一个WPF位图的事实。。。现在,我如何从数据库加载此图像并将其保存到硬盘驱动器?更新的答案,让您知道如何将字节数组转换回位图图像。有很多方法可以在以后将位图图像保存为文件。这里的示例您可以帮助我如何在“保存”按钮中将上一个转换代码字节合并为位图图像,以便将图像直接从数据库保存到特殊文件夹。我试了很多,但没有结果。我创建了一个SaveFileDialogBitmapEncoder编码器=新的PngBitmapEncoder;encoder.Frames.AddBitmapFrame.Createimage;使用var fileStream=new System.IO.FileStreamSaveFileDialog.Filename,System.IO.FileMode.Create{encoder.SavefileStream;}