如何从sqlce数据库中检索图像并放在图片框上

时间:2014-12-27 03:31:24

标签: vb.net image sql-server-ce

我之前从未使用过图像,这是我第一次。我正在尝试编写一个基于产品ID重新映射图像的过程。我做了同样的事我会检索一个字符串,但我在我的一行上收到以下错误:

Value of type '1-dimensional array of Byte' cannot be converted to 'System.Data.DataTable'

这是我的代码:

        Try

        'Set variables to get values
        Dim strCatId As String = txtCategoryName.Text
        **Dim table As DataTable = PRODUCT_CATEGORYTableAdapter.GetCategoryImage(strCatId) 'I get the error here**

        Dim imgCategory As Image = CType(table.Rows(0)("Image"), Image)

        'Send value to picturebox
        picCategoryImage.Image = imgCategory

        'Update last assigned items
        table.Rows.Clear()

    Catch ex As Exception

    End Try

因为我从未使用过图像,所以我真的不知道如何解决这个问题。有人可以帮我解决这个问题吗?

由于

1 个答案:

答案 0 :(得分:1)

我有一个功能,但这个功能适用于WPF。只需将ImageSource更改为Windows窗体中的图像。

 public static ImageSource CreateImage(string path)
    {
        System.Windows.Controls.Image finalImage = new System.Windows.Controls.Image();
        BitmapImage img = new BitmapImage();

        try
        {
            img.BeginInit();
            img.StreamSource = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        }

        catch 
        {
            return null;
        }

        finally
        {
            img.EndInit();
            finalImage.Source = img;

        }

        return finalImage.Source;
    }

}

Idea将图像写入文件。然后将其转换为BitMap Image

相关问题