在c#中写入图像时文件格式错误

时间:2015-09-10 21:55:49

标签: c# image linq memorystream

我有一个C#应用程序从MS访问数据库中读取图像并从ole对象写入二进制数据并将其写入保存图像的相同图像扩展名,但问题是文件写入格式错误。 这是代码:

 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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

为什么要打开流并循环遍历字节等?

您可以尝试以下方法吗?

File.WriteAllBytes(imageSavePath, fetchedimgbytes );

取代

      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);
                }
            }
        }

并查看文件是否正确保存?

所有这一切都假设您下载的图片.bmp确实是.bmp个文件。