如何在 C# 中从数据库检索时将 varBinary 转换为图像或视频

发布于 2024-10-31 17:50:04 字数 1321 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时间海 2024-11-07 17:50:04

由于图像以二进制格式存储在数据库中,因此您希望通过利用 MemoryStream 对象将其“流”到图像对象中。

查看您的代码,您的解决方案将如下所示:

BitmapImage bmpImage = new BitmapImage();
MemoryStream msImageStream = new MemoryStream();    

msImageStream.Write(value, 0, value.Length);

bmpCardImage.BeginInit();
bmpCardImage.StreamSource = new MemoryStream(msImageStream.ToArray());
bmpCardImage.EndInit();

image.Source = bmpCardImage;

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:

BitmapImage bmpImage = new BitmapImage();
MemoryStream msImageStream = new MemoryStream();    

msImageStream.Write(value, 0, value.Length);

bmpCardImage.BeginInit();
bmpCardImage.StreamSource = new MemoryStream(msImageStream.ToArray());
bmpCardImage.EndInit();

image.Source = bmpCardImage;
⊕婉儿 2024-11-07 17:50:04

这很简单,如果您有二进制数据并想要创建 Image 对象,请使用以下代码:

public Image BinaryToImage(byte[] binaryData)
{
     MemoryStream ms = new MemoryStream(binaryData);
     Image img = Image.FromStream(ms);
     return img;
}

It's very easy, if you have a binary data and want to create an Image object, use this code:

public Image BinaryToImage(byte[] binaryData)
{
     MemoryStream ms = new MemoryStream(binaryData);
     Image img = Image.FromStream(ms);
     return img;
}
一袭水袖舞倾城 2024-11-07 17:50:04

如果您已经有了字节,要验证您保存的内容是否正确,您可以将字节保存到文件中并打开它......

string tempFile = Path.GetTempFileName();
MemoryStream ms = new MemoryStream(bytes); //bytes that was read from the db
//Here I assume that you're reading a png image, you can put any extension you like is a file name
FileStream stream = new FileStream(tempFile + ".png", FileMode.Create);
ms.WriteTo(stream);
ms.Close();
stream.Close();
//And here we open the file with the default program
Process.Start(tempFile + ".png");

稍后您可以使用 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....

string tempFile = Path.GetTempFileName();
MemoryStream ms = new MemoryStream(bytes); //bytes that was read from the db
//Here I assume that you're reading a png image, you can put any extension you like is a file name
FileStream stream = new FileStream(tempFile + ".png", FileMode.Create);
ms.WriteTo(stream);
ms.Close();
stream.Close();
//And here we open the file with the default program
Process.Start(tempFile + ".png");

And later you can use the answer of Dillie-O and stream....

横笛休吹塞上声 2024-11-07 17:50:04

Dillie-O 的代码构成了一个非常好的扩展方法:

    // from http://stackoverflow.com/questions/5623264/how-to-convert-varbinary-into-image-or-video-when-retrieved-from-database-in-c:
    public static BitmapImage ToImage(this Binary b)
    {
        if (b == null)
            return null;

        var binary = b.ToArray();
        var image = new BitmapImage();
        var ms = new MemoryStream();

        ms.Write(binary, 0, binary.Length);

        image.BeginInit();
        image.StreamSource = new MemoryStream(ms.ToArray());
        image.EndInit();

        return image;
    }

Dillie-O's code makes for a very nice extension method:

    // from http://stackoverflow.com/questions/5623264/how-to-convert-varbinary-into-image-or-video-when-retrieved-from-database-in-c:
    public static BitmapImage ToImage(this Binary b)
    {
        if (b == null)
            return null;

        var binary = b.ToArray();
        var image = new BitmapImage();
        var ms = new MemoryStream();

        ms.Write(binary, 0, binary.Length);

        image.BeginInit();
        image.StreamSource = new MemoryStream(ms.ToArray());
        image.EndInit();

        return image;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文