如何使用实体数据模型将图像从图像控件插入 WPF 到 SQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18880164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert image from image control into WPF to SQL Database using entity data model
提问by Mohamed Safwat
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 中,我想知道如何使用实体框架将图像从 WPF 中的图像控件插入到 SQL 数据库中
i made event to upload image into image control and now i need to save it to sql database using entity framework
我制作了将图像上传到图像控件的事件,现在我需要使用实体框架将其保存到 sql 数据库
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));
}
}
this is my database named cv
这是我的名为 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 ?
如何将图像从图像控件保存到数据库中?
thanks;
谢谢;
回答by Clemens
You will most certainly store an encoded image as byte[]. The following method creates a PNG frame from a BitmapSource:
您肯定会将编码图像存储为byte[]. 以下方法从 BitmapSource 创建一个 PNG 框架:
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();
}
}
As you put BitmapImages to your Image's Source, you may simply pass that to this method:
当您将 BitmapImages 放入图像的源时,您可以简单地将其传递给此方法:
var imageBuffer = BitmapSourceToByteArray((BitmapSource)photo.Source);
回答by Pawel
Image in SqlServer is "Variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes". In EF you need to have a property of byte[]type which can be mapped to a column of imagetype in SqlServer. When you load your image from disk don't load it to the BitmapImage but to a byte array, set the property accordingly and .SaveChanges()to store it in the database. If you want to show the image to the user load it from the database as byte array and then create a BitmapImage instance. You probably would have to use BimatpImage.StreamSourceproperty to do that.
SqlServer 中的图像是“从 0 到 2^31-1 (2,147,483,647) 字节的可变长度二进制数据”。在 EF 中,您需要有一个byte[]type属性,该属性可以映射到imageSqlServer 中的类型列。当您从磁盘加载图像时,不要将其加载到 BitmapImage 而是加载到字节数组,相应地设置属性并将.SaveChanges()其存储在数据库中。如果要向用户显示图像,则将其作为字节数组从数据库中加载,然后创建一个 BitmapImage 实例。你可能不得不使用BimatpImage.StreamSource财产来做到这一点。

