如何在 C# 中从数据库检索时将 varBinary 转换为图像或视频
我正在使用 Visual Studio 2010(桌面应用程序)并使用 LINQ to SQL 将图像/视频或音频文件保存到数据类型 VarBinary (MAX)
的数据库中。我可以做到这一点...问题是,我无法获取它们并在 xaml 中显示它们,因为我无法正确获取转换部分。这是我到目前为止所拥有的(尽管它不起作用);
private void bt_Click (object sender, RoutedEventArgs e)
{
databaseDataContext context = new databaseDataContext();
var imageValue = from s in context.Images
where s.imageID == 2
select s.imageFile;
value = imageValue.Single().ToString();
//convert to string and taking down to next method to get converted in image
}
public string value { get; set; }
public object ImageSource //taking from http://stackoverflow.com/
{
get
{
BitmapImage image = new BitmapImage();
try
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(value, UriKind.Absolute);
image.EndInit();
Grid.Children.Add(image);
}
catch { return DependencyProperty.UnsetValue; } return image;
}
}
我什至不确定我是否走在正确的轨道上?我假设视频或音频是非常相似的方法?
I am using visual studio 2010, (desktop application) and using LINQ to SQL to save image/video or audio files to database in dataType VarBinary (MAX)
. This I can do... Problem is, I can't get them and display them in xaml because I can't get the converting part correct. Here is what I have so far (though its not working);
private void bt_Click (object sender, RoutedEventArgs e)
{
databaseDataContext context = new databaseDataContext();
var imageValue = from s in context.Images
where s.imageID == 2
select s.imageFile;
value = imageValue.Single().ToString();
//convert to string and taking down to next method to get converted in image
}
public string value { get; set; }
public object ImageSource //taking from http://stackoverflow.com/
{
get
{
BitmapImage image = new BitmapImage();
try
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(value, UriKind.Absolute);
image.EndInit();
Grid.Children.Add(image);
}
catch { return DependencyProperty.UnsetValue; } return image;
}
}
I not even sure if I am on the correct track? And I am assuming that video or audio is quite similar methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于图像以二进制格式存储在数据库中,因此您希望通过利用 MemoryStream 对象将其“流”到图像对象中。
查看您的代码,您的解决方案将如下所示:
Since your image is stored in binary format in the database, you want to "stream" this into an image object by leveraging the MemoryStream object.
Looking at your code, your solution will look something like this:
这很简单,如果您有二进制数据并想要创建 Image 对象,请使用以下代码:
It's very easy, if you have a binary data and want to create an Image object, use this code:
如果您已经有了字节,要验证您保存的内容是否正确,您可以将字节保存到文件中并打开它......
稍后您可以使用 Dillie-O 的答案和流......
If you already have the bytes, to verify that what you saved is correct you can save the bytes to a file and open it....
And later you can use the answer of Dillie-O and stream....
Dillie-O 的代码构成了一个非常好的扩展方法:
Dillie-O's code makes for a very nice extension method: