如何将图像从DevExpress gridControl中的RepositoryItemPictureEdit保存到sql数据库

时间:2019-03-11 07:41:44

标签: c# sql-server winforms devexpress

我曾经使用此代码将图像从DevExpress PictureEdit保存到SQL数据库

byte[] Picture;
MemoryStream ms = new MemoryStream();
pictureEdit.Image.Save(ms, ImageFormat.Jpeg);
Picture = ms.ToArray();

现在我有gridControl,其中一列为“图像”

void CreateImageTable()
    {
        dtimage.Columns.Add("Image", typeof(Image));

        gridControl4.DataSource = dtimage;
    }

并且我使用此代码将图像添加到gridControl

        private void simpleButton1_Click(object sender, EventArgs e)
    {
        var ofd = new XtraOpenFileDialog();
        ofd.Multiselect = true;
        ofd.Filter = "Image|*.JPG;*.BMP;*.PNG;*.JPEG;*.GIF";
        ofd.Title = Resources.insertImage;
        ofd.ShowDialog();
        for (int z = 0; z < ofd.FileNames.Length; z++)
        {
            Image img = Image.FromFile(ofd.FileNames[z]);
            DataRow row = dtimage.NewRow();
            row[0] = img;
            dtimage.Rows.Add(row);
        }
    }

现在我创建`

  RepositoryItemPictureEdit imgdaged = new RepositoryItemPictureEdit();
  gridControl4.RepositoryItems.Add(imgdaged);
        imgdaged.SizeMode = PictureSizeMode.Zoom;
        imgdaged.NullText = " ";
        imgdaged.PictureStoreMode = PictureStoreMode.ByteArray;
        imgdaged.ContextMenuStrip = new ContextMenuStrip();
        gridView4.Columns["Image"].ColumnEdit = imgdaged;

但是当我尝试使用此代码时

imgdaged.Image.Save(ms, ImageFormat.Jpeg);

我收到此错误:CS1061'RepositoryItemPictureEdit'不包含'Image'的定义,并且找不到可访问的扩展方法'Image'接受类型的第一个参数(您是否缺少using指令或程序集引用? ) 这是我执行存储过程的代码

public void AddRawMaterialRecommendationImage(int FK_OrderNumber, string UserID, DateTime CreationDate,byte[] RawDamagedimage)
    {
        DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
        DAL.Open();
        SqlParameter[] param = new SqlParameter[4];

        param[0] = new SqlParameter("@FK_OrderNumber", SqlDbType.Int);
        param[0].Value = FK_OrderNumber;

        param[1] = new SqlParameter("@UserID", SqlDbType.NVarChar, 50);
        param[1].Value = UserID;

        param[2] = new SqlParameter("@CreationDate", SqlDbType.Date);
        param[2].Value = CreationDate;

        param[3] = new SqlParameter("@RawDamagedimage", SqlDbType.Image);
        param[3].Value = RawDamagedimage;

        DAL.ExcuteCommande("AddRawMaterialRecommendationImage", param);
        DAL.Close();

    }

我尝试使用此代码,但不幸的是对我不起作用

for (int i = 0; i < dtimage.Rows.Count; i++)
            {
                var row = dtimage.Rows[i];
                byte[] imageBytes = Encoding.Unicode.GetBytes(row["Image"].ToString());
                MemoryStream ms = new MemoryStream();
                imageBytes = ms.ToArray();
                damaged.AddRawMaterialRecommendationImage(Convert.ToInt32(txtBon.Text), Program.UserID, DateTime.Now, imageBytes);
            }

我该如何解决这个问题,谢谢。

0 个答案:

没有答案