且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用实体数据模型将图像从图像控件插入WPF到SQL数据库

更新时间:2022-02-26 02:57:15

你绝对会将编码的图像存储为 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);