如何使用实体数据模型插入​​来自图像控制图像转换成WPF到SQL数据库图像、转换成、如何使用、实体

2023-09-05 03:44:26 作者:つ ℡。滚

我创建一个应用程序,以学生的信息保存到SQL,我想知道如何使用实体框架的SQL数据库中插入图像,从图像控制在WPF

i am creating an app to save student information into SQL , I want to know how to insert image from image control in WPF using entity framework to SQL database

我犯了事件上传图像到影像控制,现在我需要通过将其保存到SQL数据库实体框架

i made event to upload image into image control and now i need to save it to sql database using entity framework

图像加载按钮code:

image load button code :

private void uploadbt_Click(object sender, RoutedEventArgs e)
 {
     OpenFileDialog op = new OpenFileDialog();
     op.Title = "Select a picture";
     op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
         "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
         "Portable Network Graphic (*.png)|*.png";
     if (op.ShowDialog() == true)
     {
         photo.Source = new BitmapImage(new Uri(op.FileName));
     }  
 }

这是我的数据库名为CV

this is my database named cv

这是我的code保存一些信息到数据库,这COSE的保存按钮

this is my code to save some information into database this cose for save button

facultymakerEntities1 entity = new  facultymakerEntities1();

         cv CV = new cv();
         CV.Full_Name = fullnametb.Text;
         CV.Activities = activitestb.Text;
         CV.Address = addresstb.Text;
         CV.Birth_Day = bddate.SelectedDate.Value;
         CV.Courses = cousetb.Text;
         CV.E_Mail = emailtb.Text;

         entity.cvs.Add(CV);
         entity.SaveChanges();

我怎样才能挽救图像控制图像到数据库?

how can i save image from image control into database ?

感谢;

推荐答案

您肯定会存储连接codeD映像作为字节[] 。下面的方法从一个的BitmapSource创建PNG-CN codeR缓存:

You will most certainly store an encoded image as byte[]. The following method create an PNG-encoder buffer from a BitmapSource:

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

当你把BitmapImages到图像的源代码,你可以简单地传递给此方法:

As you put BitmapImages to your Image's Source, you may simply pass that to this method:

var imageBuffer = BitmapSourceToByteArray((BitmapSource)photo.Source);