Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

By the way,here's the code i used to insert the data :


C#
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();
    }


Please help me out!

What I have tried:

I tried almost all the solutions available on the net but with no luck..One solution that i couldn't try was to convert the memorystream to a writable bitmap as i can't get the assembly "System.windows.graphics"
Posted
Updated 25-Feb-18 1:45am
Comments
Richard Deeming 26-Feb-18 10:59am    
You're loading multiple PNG images from the database, appending them all to the same stream, and then trying to load the combined images as a single image. That's never going to work.

1 solution

Just wild guesses, but here are a few things you could try:

"Select * from recents"
It is not a very good practice to use SELECT * in a query. If you only are interested in the Pic column of the recents table, then write
"SELECT Pic FROM recents"
It will be much cleaner, and easier to read and to debug.

C#
reader.Read();
while (reader.Read()) { /* ... */ }

When you do that, you are litteraly reading the first record and immediately discarding it. Simply write
C#
while (reader.Read()) { /* ... */ }


Another issue is that you never dispose of the stream resources you create. This can lead to problems when your method is accessed multiple times.
For example, when you are instantiating a stream, instead of
C#
MemoryStream ms = new MemoryStream();
// ...

write
C#
using (MemoryStream ms = new MemoryStream()) {
   // ...
}
Here I am showing you an example for a MemoryStream object, but this also stands for other object which implement the IDisposable interface.
IDisposable Interface[^]

Another (minor, non-blocking) issue is the way you are declaring your buffer:
C#
byte[] outByte = new byte[bufferSize + 1];
Why giving your buffer one element more than it really needs?
C#
byte[] outByte = new byte[bufferSize];
is sufficient.
The same stands for the moment you exit you while loop. Instead of
C#
writer.Write(outByte, 0, (int)retval - 1);
write
C#
if (retval > 0) { // If something remains to be written
   writer.Write(outByte, 0, (int)retval);
}


As a final note, I would say that your issue seems to come from (but that is just a guess, as you did not state which line is raising the error) the line
C#
image1.Source = System.Windows.Media.Imaging.BitmapFrame.Create(stream);
Not knowing the exact type of the image1 variable, I could only guess the real problem. Please explicit what type is the image1 variable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900