0

I have a C# application that reads images form a MS-access database and write the binary data from ole object and write it to the same image extension of the saved image but the problem is that the file is written in wrong format . here is the code:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        OleDbConnection Dbconnection = new OleDbConnection();
        OleDbDataAdapter Dataadapter;
        DataTable localDataTable = new DataTable();
        int rowposition = 0;
        int rownumber = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void ConnectToDatabase()
        {
            Dbconnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database.mdb";
            Dbconnection.Open();
            Dataadapter = new OleDbDataAdapter("SELECT * FROM `mody`", Dbconnection);
            Dataadapter.Fill(localDataTable);

            MessageBox.Show(localDataTable.Rows.Count.ToString());
            if (localDataTable.Rows.Count != 0)
            {
                rowposition = localDataTable.Rows.Count;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ConnectToDatabase();
        }

        private void btnToPB_Click(object sender, EventArgs e)
        {
            rownumber = 0;
            pictureBox1.Image = ReadImageFromDB();
            btnNext.Enabled = true;
            btnPrev.Enabled = true;
        }
        private Image ReadImageFromDB()
        {
            Image fetchedImg=null;
            if (rownumber >= 0)
            {
                byte[] fetchedimgbytes = (byte[])localDataTable.Rows[rownumber]["Img"];
                string path = @"myimage.bmp";
                using (MemoryStream inputStream = new MemoryStream(fetchedimgbytes))
                {
                    using (Stream file = File.Create(path))
                    {
                        byte[] buffer = new byte[8 * 1024];
                        int len;
                        while ((len = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            file.Write(buffer, 0, len);
                        }
                    }
                }
                return fetchedImg;
            }
            else
            {
                MessageBox.Show("no image");
                return null;
            }
        }
    }
}
Adam Miller
  • 767
  • 1
  • 9
  • 22
mohamed
  • 135
  • 1
  • 14
  • 1
    You're saving it to bmp. Is it a bmp? And how is it stored? In some cases, meta information is stored with the image data, so the one reading it can determine the type from the data. If so, you may have to strip this extra information. – GolezTrol Sep 10 '15 at 21:59
  • it is stored by filling a row in the access database and yes it is a bmp and if there is extra info how to strip it – mohamed Sep 10 '15 at 22:02
  • I think this question may answer your question. You might need to use the .NET Image methods to get the image pulled out of the memory stream properly: http://stackoverflow.com/questions/9173904/bytearray-to-image-conversion – Adam Miller Sep 10 '15 at 22:02
  • It might also help us to know the exact text of the error you're getting. – Adam Miller Sep 11 '15 at 13:43

1 Answers1

1

Why are you opening stream and looping through bytes etc?

Can you try the following?

File.WriteAllBytes(imageSavePath, fetchedimgbytes );

in place of

      using (MemoryStream inputStream = new MemoryStream(fetchedimgbytes))
        {
            using (Stream file = File.Create(path))
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    file.Write(buffer, 0, len);
                }
            }
        }

and see if the file is saved properly?

All this is assuming that the image you are downloading as .bmp is indeed a .bmp file.

Shiva
  • 20,575
  • 14
  • 82
  • 112
  • problem is not in the way of writing problem is that there is more data coming back the original size of the image is 518,538 bytes the retrived on is 518,554 bytes that is what causing the problem – mohamed Sep 10 '15 at 22:12
  • You should rephrase your question then. Right now it says "but the problem is that the file is written in wrong format " implying that there is something wrong with the way the File is being written onto disk. – Shiva Sep 10 '15 at 22:17