Home > Database > Mysql Tutorial > body text

把图片保存到数据库中和从数据库中读取图片

WBOY
Release: 2016-06-07 14:59:37
Original
1011 people have browsed it

最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。 1、将图片作为其中的一个参数保存到数据库中 在项目中,一般是将图

  最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。

 

  1、将图片作为其中的一个参数保存到数据库中

  在项目中,一般是将图片转换成二进制流格式,然后保存到数据库中。同时数据库表中存储图片的格式一般为image。此次项目,是将图片作为一个参数,和其他几个参数一起保存到数据库中,和在网上搜索到的图片保存不太一样,此处稍作修改,但都是检测过的。

  存储步骤:

  1、搜索到图片的路径

  2、读取图片并将图片转换成二进制流格式

  3、sql语句保存到数据库中。

   贴代码: 

<span>private</span> <span>void</span> btnWrite_Click(<span>object</span><span> sender, EventArgs e)
        {
            OpenFileDialog ofd </span>= <span>new</span><span> OpenFileDialog();
            ofd.Filter </span>= <span>"</span><span>*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP</span><span>"</span><span>;

            </span><span>if</span> (ofd.ShowDialog() ==<span> DialogResult.OK)
            {
                </span><span>string</span> filePath = ofd.FileName;<span>//</span><span>图片路径</span>
                FileStream fs = <span>new</span><span> FileStream(filePath, FileMode.Open);
                </span><span>byte</span>[] imageBytes = <span>new</span> <span>byte</span><span>[fs.Length];
                BinaryReader br </span>= <span>new</span><span> BinaryReader(fs);
                imageBytes </span>= br.ReadBytes(Convert.ToInt32(fs.Length));<span>//</span><span>图片转换成二进制流</span>

                <span>string</span> strSql = <span>string</span>.Format(<span>"</span><span>insert into [SBS].[dbo].[Model] ([M_QRCode],[M_Skills] ) values (@image,'2')</span><span>"</span><span>);
                </span><span>int</span> count =<span> Write(strSql,imageBytes );

                </span><span>if</span> (count > <span>0</span><span>)
                {
                    MessageBox.Show(</span><span>"</span><span>success</span><span>"</span><span>);
                }
                </span><span>else</span><span>
                {
                    MessageBox.Show(</span><span>"</span><span>failed</span><span>"</span><span>);
                }
            }
        }</span>
Copy after login

  数据库连接和保存图片语句:

把图片保存到数据库中和从数据库中读取图片把图片保存到数据库中和从数据库中读取图片

<span>private</span> <span>int</span> Write(<span>string</span> strSql,<span>byte</span><span>[] imageBytes)
        {
            </span><span>string</span> connStr = <span>"</span><span>Data Source=192.168.4.132;initial Catalog=SBS;User ID=sa;Password=sa;</span><span>"</span><span>;

            </span><span>using</span> (SqlConnection conn = <span>new</span><span> SqlConnection(connStr))
            {
                </span><span>using</span> (SqlCommand cmd = <span>new</span><span> SqlCommand(strSql, conn))
                {
                    </span><span>try</span><span>
                    {
                        conn.Open();
                        SqlParameter sqlParameter </span>= <span>new</span> SqlParameter(<span>"</span><span>@image</span><span>"</span><span>, SqlDbType.Image);
                        sqlParameter.Value </span>=<span> imageBytes;
                        cmd.Parameters.Add(sqlParameter);
                        </span><span>int</span> rows =<span> cmd.ExecuteNonQuery();
                        </span><span>return</span><span> rows;
                    }
                    </span><span>catch</span><span> (Exception e)
                    {
                        </span><span>throw</span><span>;
                    }
                }
            }
        }</span>
Copy after login
View Code

 

  2、从数据库总读取图片

  从数据库中读取图片字段,并转换成内存流生成bitmap。

  贴代码: 

<span>private</span> <span>void</span> btnRead_Click(<span>object</span><span> sender, EventArgs e)
        {
            </span><span>string</span> strSql = <span>string</span>.Format(<span>"</span><span>select M_QRCode from [SBS].[dbo].[Model] where M_id = 7</span><span>"</span>);<span>//</span><span>图片保存的字段是M_QRCode</span>
<span>            Read(strSql);
        }

        </span><span>private</span> <span>void</span> Read(<span>string</span><span> strSql)
        {
            </span><span>string</span> connStr = <span>"</span><span>Data Source=192.168.4.132;initial Catalog=SBS;User ID=sa;Password=sa;</span><span>"</span><span>;

            </span><span>using</span> (SqlConnection conn = <span>new</span><span> SqlConnection(connStr))
            {
                </span><span>using</span> (SqlCommand cmd = <span>new</span><span> SqlCommand(strSql, conn))
                {
                    conn.Open();
                    SqlDataReader sqlDr </span>=<span> cmd.ExecuteReader();
                    sqlDr.Read();
                    </span><span>byte</span>[] images = (<span>byte</span>[])sqlDr[<span>"</span><span>M_QRCode</span><span>"</span><span>];
                    MemoryStream ms </span>= <span>new</span><span> MemoryStream(images);
                    Bitmap bmp </span>= <span>new</span><span> Bitmap(ms);
                    pictureBox1.Image </span>=<span> bmp;
                }
            }
        }</span>
Copy after login

 

  3、根据图片路径显示图片

  这个比较简单,直接贴出代码 

<span>private</span> <span>void</span> btnLoad_Click(<span>object</span><span> sender, EventArgs e)
        {
            OpenFileDialog ofd </span>= <span>new</span><span> OpenFileDialog();
            ofd.Filter </span>= <span>"</span><span>*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP</span><span>"</span><span>;
            </span><span>if</span> (ofd.ShowDialog() ==<span> DialogResult.OK)
            {
                pictureBox1.Image </span>=<span> Image.FromFile(ofd.FileName);
            }
        }</span>
Copy after login

 

  4、打印图片

  打印图片是在将图片显示在pictureBox的基础上进行的。

  步骤:

  1、将printDocument控件拖到界面,添加打印代码

  2、设置PrintDocument控件的Print_PrintPage事件

<span>private</span> <span>void</span> btnPrint_Click(<span>object</span><span> sender, EventArgs e)
        {
            PrintDialog printDialog </span>= <span>new</span><span> PrintDialog();
            printDialog.Document </span>= <span>this</span><span>.printDocument1;
            </span><span>if</span> (printDialog.ShowDialog() ==<span> DialogResult.OK)
            {
                </span><span>try</span><span>
                {
                    printDocument1.Print();
                }
                </span><span>catch</span><span> (Exception ex)
                {
                   printDocument1.PrintController.OnEndPrint(printDocument1, </span><span>new</span><span> System.Drawing.Printing.PrintEventArgs());
                }
            }
        }

        </span><span>private</span> <span>void</span> printDocument1_PrintPage(<span>object</span><span> sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(pictureBox1.Image, </span><span>30</span>, <span>30</span><span>);
        }</span>
Copy after login

  

  附带着将图片转换成二进制和将二进制转换成图片专门写出来,以便于查看。 

 <span>public</span> <span>byte</span>[] ConvertBinary(<span>string</span><span> filePath)
        {
            FileStream fs </span>= <span>new</span> FileStream(filePath, FileMode.Open, FileAccess.Read);<span>//</span><span>以文件流形式读取图片</span>
            BinaryReader br = <span>new</span> BinaryReader(fs);<span>//</span><span>转换成二进制流</span>
            <span>byte</span>[] imageBytes = br.ReadBytes((<span>int</span>)fs.Length);<span>//</span><span>保存到字节数组中</span>

            <span>return</span><span> imageBytes;
        }

        </span><span>public</span> <span>void</span> ShowImage(<span>byte</span><span>[] imageBytes)
        {
            MemoryStream ms </span>= <span>new</span><span> MemoryStream(imageBytes);
            pictureBox1.Image </span>=<span> Image.FromStream(ms);
        }</span>
Copy after login

 

  在pictureBox中显示图片的三种方式: 

<span>public</span> <span>void</span><span> Method()
        {
            MemoryStream ms;
            pictureBox1.Image </span>=<span> Image.FromStream(ms);

            Bitmap bitmap;
            pictureBox1.Image </span>=<span> bitmap;

            </span><span>string</span><span> filePath;
            pictureBox1.Image </span>=<span> Image.FromFile(filePath);
        }</span>
Copy after login

 

  winform中控件combobox控件使用: 

<span>public</span> <span>void</span><span> BindCombobox()
        {
            DataTable dt </span>= <span>new</span><span> DataTable();
            dt.Columns.Add(</span><span>new</span> DataColumn(<span>"</span><span>id</span><span>"</span>, <span>typeof</span>(<span>int</span><span>)));
            dt.Columns.Add(</span><span>new</span> DataColumn(<span>"</span><span>value</span><span>"</span>, <span>typeof</span>(<span>string</span><span>)));

            </span><span>for</span> (<span>int</span> i = <span>0</span>; i 3; i++<span>)
            {
                DataRow dr </span>=<span> dt.NewRow();
                dr[</span><span>"</span><span>id</span><span>"</span>] =<span> i;
                dr[</span><span>"</span><span>value</span><span>"</span>] = <span>10</span> +<span> i;
                dt.Rows.Add(dr);
            }

            </span><span>this</span>.comboBox1.DataSource =<span> dt;
            </span><span>this</span>.comboBox1.DisplayMember = <span>"</span><span>value</span><span>"</span><span>;
            </span><span>this</span>.comboBox1.ValueMember = <span>"</span><span>id</span><span>"</span><span>; 
        }

        </span><span>public</span> <span>void</span><span> ShowValue()
        {
            </span><span>this</span>.textBox1.Text = <span>this</span><span>.comboBox1.Text;
            </span><span>this</span>.textBox2.Text = <span>this</span><span>.comboBox1.SelectedValue.ToString();
        }</span>
Copy after login

 

  以上就是一些琐碎的总结,谨作为日后学习工作使用。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template