【问题标题】:How can I add an image in my database sqlite using dbContext WPF c#?如何使用 dbContext WPF c# 在我的数据库 sqlite 中添加图像?
【发布时间】:2019-11-13 20:38:34
【问题描述】:

我想以字节形式将图像添加到数据库中的图像列中 我正在使用 SQLite 保存我的数据库数据和 WPF 应用程序使用 dbContext c# 来编写我的代码

谁能帮帮我?

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

【问题讨论】:

    标签: c# wpf sqlite dbcontext


    【解决方案1】:

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

    【讨论】:

    • 一个问题请...现在我如何从数据库中加载这张图片并将其保存到硬盘上?
    • 更新了答案,让您知道如何将字节数组转换回位图图像。之后有很多方法可以将该位图图像保存为文件。
    • 你能帮我吗,如何将你最后的转换代码(字节到 BitmapImage )合并到保存按钮中,以将图像直接从数据库保存到特殊文件夹。我尝试了很多但没有得到结果。我创建了一个 SaveFileDialog
    • BitmapEncoder 编码器 = new PngBitmapEncoder();编码器.Frames.Add(BitmapFrame.Create(image));使用 (var fileStream = new System.IO.FileStream(SaveFileDialog.Filename, System.IO.FileMode.Create)) { encoder.Save(fileStream); }
    【解决方案2】:

    在“零件”类中添加属性

    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 和 BitmapImage 至少是误导,如果没有错的话。
    • 很好。这仍然不是您在 WPF 中执行此操作的方式。
    • 也就是说,在您的示例中,创建位图根本没有意义。您只想从文件中获取图像字节,所以part.ImageCol = File.ReadAllBytes(openFileDialog.FileName); 应该足够了。
    • 好的,但在我的示例中,我进一步使用了位图对象。所以我先把它转换成位图:)
    • 也许吧,但您完全忽略了 OP 已经拥有 WPF BitmapImage 的事实。
    猜你喜欢
    • 2021-02-03
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多