检索、调整大小并将图像保存回 Access 数据库图像字段 [英] Retrieve, Resize, and Save Images Back to Access Database Image Field

查看:27
本文介绍了检索、调整大小并将图像保存回 Access 数据库图像字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中有一个照片列,其中一个用户决定开始将 20MB 的图像保存到该字段中,结果导致数据库在不到一年的时间内增加了 6 倍.所以我的最终目标是遍历表格,拉取图像,调整大小并裁剪,然后将其保存回数据库.

所以我最初的想法是将这些数据保存为 blob 字节数组,所以我正在尝试:

public static Bitmap ByteToImage(byte[] blob){使用 (var mStream = new MemoryStream()){mStream.Write(blob, 0, blob.Length);mStream.Seek(0, SeekOrigin.Begin);var bm = new Bitmap(mStream);返回 bm;}}//方法const string myConnectionString = @"Driver={Microsoft Access Driver (*.mdb)}; Dbq=E:\SUFDB2006NoPics.mdb;Uid=Admin;Pwd=;";使用(var myConnection = new OdbcConnection()){myConnection.ConnectionString = myConnectionString;myConnection.Open();var dadapter = new OdbcDataAdapter("Select [Contact ID#], [Photo] FROM Contact", myConnection);var table = new DataTable();dadapter.Fill(table);foreach(table.Rows 中的 DataRow 行){var pht = (byte[])row["Photo"];//试过这个1var stream = new MemoryStream(pht);//参数无效错误.pictureBox1.Image = Image.FromStream(stream);//试过这个2图片框1.Image = ByteToImage(pht);}this.dataGridView1.DataSource = table;//不过这样没问题dataGridView1.Refresh();}

所以,有趣的是图像列绑定到数据网格视图没问题.但是,当我尝试将单个图像行绑定到图片框时,它会引发错误.我会把二进制文件放在上面,但它的方式很长.图像没有损坏,因为它们在 datagridview 中没有问题.

我这样做对吗?以 blob 形式存储的数据是否与在 SQL Server 数据库中的存储方式相同?此外,如果有人知道一种更简单的方法来做到这一点,那就太好了.谢谢

编辑

文本文件中的二进制数据.太大不能放在这里有一些链接可以帮助你.>

I have a table that has a photo column in it, one of the users decided to start saving 20MB images into the field, which consequently caused the database to jump up in size by a factor of 6 in less than a year. So my ultimate goal is to iterate through the table, pull the image, resize and crop it, then save it back to the database.

So my initial thought was this data is being saved as a blob byte array so i'm trying:

public static Bitmap ByteToImage(byte[] blob)
{
    using (var mStream = new MemoryStream())
    {
        mStream.Write(blob, 0, blob.Length);
        mStream.Seek(0, SeekOrigin.Begin);
        var bm = new Bitmap(mStream);
        return bm;
    }
}

//Method
const string myConnectionString = @"Driver={Microsoft Access Driver (*.mdb)};  Dbq=E:\SUFDB2006NoPics.mdb;Uid=Admin;Pwd=;";

using(var myConnection = new OdbcConnection())
{
    myConnection.ConnectionString = myConnectionString;
    myConnection.Open();
    var dadapter = new OdbcDataAdapter("Select [Contact ID#], [Photo] FROM Contact", myConnection);
    var table = new DataTable();
    dadapter.Fill(table);

    foreach (DataRow row in table.Rows)
    {
       var pht = (byte[])row["Photo"];

       //Tried this 1
       var stream = new MemoryStream(pht); //Parameter is not valid Error.
       pictureBox1.Image = Image.FromStream(stream);

       //Tried this 2
       pictureBox1.Image = ByteToImage(pht);

    }

    this.dataGridView1.DataSource = table; //However this works no problem
    dataGridView1.Refresh();
}

So, interestingly the image column binds to a datagridview no problem. However when I try to bind an individual image row to a picturebox it throws an error. I would put the binary up but its WAY to long. The images are not corrupt as they pull up no problem in the datagridview.

Am i going about this the right way ? Is the data stored as a blob the same way it is in a SQL Server Database ? Also if anyone know of an easier way to do this that would be great. Thanks

Edit

Binary Data in Text File. Too Big to put here is a Link

...and a screenshot:

解决方案

I've had a look at the file you posted, and it looks you're storing your image as an OLE object rather than the bytes of an image. The OLE container prepends a header to your file, which is why you can't decode the byte array directly into an image.

In the case of the file you provided, the bitmap image starts at byte 79 with the BMP header 424D.

This answer has some links that should help you out.

这篇关于检索、调整大小并将图像保存回 Access 数据库图像字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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