没有找到适合完成此操作的成像组件.wpf c# [英] No imaging component suitable to complete this operation was found.wpf c#

查看:205
本文介绍了没有找到适合完成此操作的成像组件.wpf c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从WPF中的 .mdb 数据库加载图像。我正在使用此代码:

I'm trying to load image from my .mdb database in WPF. I'm using this code :

public void loadimg()
{
    con.Open();
    OleDbCommand cmd = new OleDbCommand("Select * from recents", con);
    DataTable table = new DataTable;
    OleDbDataAdapter adap = new OleDbDataAdapter(cmd);
    adap.Fill(table);
    if (table.Rows.Count <= 0)
    {
        MsgBox("nooo");
    }
    else
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter stm;
        BinaryWriter writer = new BinaryWriter(stream);
        int bufferSize = 100;
        byte[] outByte = new byte[bufferSize + 1];
        long retval;
        long startIndex = 0;
        string pubID = "";
        OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
        reader.Read();
        while (reader.Read())
        {
            startIndex = 0;
            retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
            while (retval == bufferSize)
            {
                writer.Write(outByte);
                writer.Flush();
                startIndex += bufferSize;
                retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
            }

            writer.Write(outByte, 0, (int)retval - 1);
            writer.Flush();
        }

        reader.Close();
        con.Close();
        stream.Position = 0;
        stream.Seek(0, SeekOrigin.Begin);
        System.Drawing.Image _Image = System.Drawing.Image.FromStream(stream);
        image1.Source = System.Windows.Media.Imaging.BitmapFrame.Create(stream);
    }
}

上面的代码会返回错误:

The code above returns an error :


找不到适合完成此操作的成像组件。

No imaging component suitable to complete this operation was found.



<我花了好几个小时试图弄清楚如何修复它。任何帮助都会受到高度赞赏。

I spent hours trying to figure out how to fix it.Any help would be highly appreciated.

更新
评论,我被问到我是否正确插入数据..好吧,这是我用来插入数据的代码:

Update In the comments, i was asked if i inserted the data properly..Well,here's the code i used to insert the data :

public void adddata()
{
con.Open();
OleDbCommand cmd = new OleDbCommand("Insert into   recents(Pic)values(@pic)", con);
byte[] data;
System.Drawing.Image myimage =  System.Drawing.Image.FromFile("E:\\19686468_1419770068104721_1127495277_o.png");
using (MemoryStream ms = new MemoryStream())
{
    myimage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    data = ms.ToArray();
}

cmd.Parameters.AddWithValue("@pic", data);
cmd.ExecuteNonQuery();
con.Close();
}

请帮帮我!

推荐答案

修正了......对于将来遇到此错误的人:

Fixed it...For anyone who faces this error in future :


  1. 确保以正确的方式插入数据(有时数据库中的数据损坏会导致此类错误)

2。您不需要做一些繁重的编码就可以将图像转换为字节!

2 . You don't need to do some heavy coding to convert the image to byte!

最后,让我们的代码:

public void loadimg()
{
    con.Open();
    OleDbCommand cmd = new OleDbCommand("Select * from recents", con);
    OleDbDataReader _dr;
    _dr = cmd.ExecuteReader;
    byte[] _photo;
    while (_dr.Read())
    {
        try
        {
            _photo = (byte[])_dr(1);
            BitmapImage bi = new BitmapImage();
            using (MemoryStream strm = new MemoryStream(_photo))
            {
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.StreamSource = strm;
                bi.EndInit();
            }
            image1.Source = bi;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

这篇关于没有找到适合完成此操作的成像组件.wpf c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆