gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:13:45 25 4
gpt4 key购买 nike

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

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com