如何使用实体数据模型将图像从图像控件插入WPF到SQL数据库 [英] How to insert image from image control into WPF to SQL Database using entity data model

查看:115
本文介绍了如何使用实体数据模型将图像从图像控件插入WPF到SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

i将图像上传到图像控件中,现在我需要使用实体框架将其保存到sql数据库中

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

图像加载按钮代码:

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

这是我的代码,将一些信息保存到数据库中,保存按钮

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 ?

谢谢;

推荐答案

你绝对会将编码的图像存储为 byte [] 。以下方法从BitmapSource创建一个PNG框架:

You will most certainly store an encoded image as byte[]. The following method creates a PNG frame 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);

这篇关于如何使用实体数据模型将图像从图像控件插入WPF到SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆