【问题标题】:How to save image in SQL image type column from picturebox without mentioning pics url?如何在不提及图片网址的情况下将图片保存在图片框中的 SQL 图像类型列中?
【发布时间】:2015-07-14 14:09:28
【问题描述】:

我有一个picturebox,在按下鼠标按钮时会绘制自定义图像现在我想将该图像保存在 SQL 的image type column 中。

我确实搜索了保存,但没有将图片从图片框保存到没有 url 的 sql 图像类型列。

【问题讨论】:

标签: c# sql sql-server image picturebox


【解决方案1】:

您或许可以使用this solution。您必须先将图片框图像保存为 jpeg。

byte[] image = File.ReadAllBytes("D:\\11.jpg");

已编辑:

    //without saving the image to a physical file
    MemoryStream stream=new MemoryStream();

pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] pic=stream.ToArray(); 


SqlCommand sqlCommand = new SqlCommand("INSERT INTO imageTest (pic_id, pic) VALUES (1, @Image)", yourConnectionReference);
sqlCommand.Parameters.AddWithValue("@Image", image);
sqlCommand.ExecuteNonQuery();

【讨论】:

  • 谢谢,但有没有办法直接从 PictureBox 存储?不是来自物理路径?
  • 是的,你可以。试试这个:MemoryStream stream=new MemoryStream(); pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg); [] pic=stream.ToArray(); SqlCommand sqlCommand = new SqlCommand("INSERT INTO imageTest (pic_id, pic) VALUES (1, @Image)", yourConnectionReference); sqlCommand.Parameters.AddWithValue("@Image", image); sqlCommand.ExecuteNonQuery();
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多