如何使用dbContext WPF c#在数据库sqlite中添加图像?

时间:2019-07-03 07:46:55

标签: 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 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();
    }
}

2 个答案:

答案 0 :(得分:2)

只需先将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();
}

答案 1 :(得分:-1)

在“零件”类中添加属性

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();
        }

将BitMap转换为字节[]

public static byte[] ConvertBitmapImagetoBytes(this Image image)
    {
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }