从SQL数据库中检索图像 [英] Retrieving a image from SQL Database

查看:107
本文介绍了从SQL数据库中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图片框(frontPictureBox)中的图像存储到名为Music的SQL DB表中。我意识到我必须将其转换为字节数组。但是当我尝试从数据库中检索它时,我最终在pictureBox中出现黑色背景。



这是代码:



I am trying to store a image from a picture box (frontPictureBox) to a SQL DB table called Music. I realize I have to convert it to a Byte Array. But when I try retrieving it from the database I end up with a black background in the pictureBox.

Here is the code:

SqlConnection con = new SqlConnection("Server=.;database=CollectionDb;integrated security=true");
           
            SqlCommand com = new SqlCommand("insert into tblMusic(fldCode,fldPic) values(1,@Pic)", con);

            
            MemoryStream stream=new MemoryStream();
            
            frontPictureBox.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

            
            byte[] pic=stream.ToArray();

            com.Parameters.AddWithValue("@Pic", pic);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }





现在程序出错了。



Now the program is throwing a error.

推荐答案

JPEG不支持透明度。所以你需要用一些颜色填充背景或者将它保存为PNG而不是JPEG。



祝你好运!
JPEG doesn't support transparency. So you need to fill the background with some color or save it as PNG instead of JPEG.

Good luck!


这篇关于从SQL数据库中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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