c# - 将图片框图像存储到数据库

标签 c# sql picturebox

我有一个图片框。图片框中的图像是静态图像。我正在从资源文件夹加载图像。我无法从图片框中读取静态图像并将其存储在数据库中。这是我的代码。

 private void btnOk_Click(object sender, EventArgs e)
    {
        Image votingBackgroundImage = pictureBox5.Image;
        Bitmap votingBackgroundBitmap = new Bitmap(votingBackgroundImage);
         Image votingImage = (Image)votingBackgroundBitmap;
            var maxheight = (votingImage.Height * 3) + 2;
            var maxwidth = votingImage.Width * 2;
            if (maxheight == 227 && maxwidth == 720)
            {

                 System.IO.MemoryStream defaultImageStream = new System.IO.MemoryStream();
                 Bitmap NewImage =new Bitmap(votingImage,new Size(720,227));
                 Image b = (Image)NewImage;
                  b.Save(defaultImageStream, System.Drawing.Imaging.ImageFormat.Bmp);
                 defaultImageData = new byte[defaultImageStream.Length];
              }
    }

我用来在数据库中添加图片的查询:

            String MyString1=string.format("insert into question_table(background_image) Values(@background_image)");
            com = new SqlCommand(MyString1, myConnection);
              com.Parameters.Add("@background_image", SqlDbType.Image).Value = defaultImageData;
                com.ExecuteNonQuery();

当我在 sql 数据库中检查它时。它将值存储为 0 x000000...

最佳答案

你只是在创建 byte[] 但你从未真正复制过内容

尝试

 System.IO.MemoryStream defaultImageStream = new System.IO.MemoryStream();
 Bitmap NewImage =new Bitmap(votingImage,new Size(720,227));
 Image b = (Image)NewImage;
 b.Save(defaultImageStream, System.Drawing.Imaging.ImageFormat.Bmp);
 defaultImageData = new byte[defaultImageStream.Length];
 //assign byte array the content of image
 defaultImageData = defaultImageStream .ToArray();

关于c# - 将图片框图像存储到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7038398/

相关文章:

c# - 如果我在 Try block 中返回值,Finally 语句中的代码会触发吗?

c# - Windows 8 GridView - 禁用项目点击可视化

java - 在数据库中存储 jXDatePicker1 日期值时出错

c# - 在图片盒中绘制颜色?

c# - 如何使图片框透明?

c# - 使用 Microsoft 报表查看器 : Request for the permission of type 'System.Security.Permissions.SecurityPermission' Failed

c# - 使用 AttachConsole,用户必须按回车键才能获得常规命令行

sql - 如何从 3 个表中选择特定数据? [PostgreSQL 9.1]

Mysql With Rollup 没有运行我预期的

c# - 如何将 PictureBox.Image 保存到文件?