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.
reader.Read();
while (reader.Read()) { }
When you do that, you are litteraly reading the first record and immediately discarding it. Simply write
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
MemoryStream ms = new MemoryStream();
write
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:
byte[] outByte = new byte[bufferSize + 1];
Why giving your buffer one element more than it really needs?
byte[] outByte = new byte[bufferSize];
is sufficient.
The same stands for the moment you exit you while loop. Instead of
writer.Write(outByte, 0, (int)retval - 1);
write
if (retval > 0) {
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
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.