如何将图片从图片盒保存到数据库 [英] How to Save a Image from picturebox to database

查看:119
本文介绍了如何将图片从图片盒保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生您好,
我是C#的实习生.最近,我遇到了将图片框中的图像(jpg格式)保存到数据库的问题.我正在使用sql数据库.和visual Studio2008.我可以在窗口应用程序中将任何图像显示到图片框.但无法将该图像保存在数据库中.

请问您能否解决我的问题.

谢谢
Shafiqul Azam博士
达卡,
孟加拉国

Hello sir,
I am a trainee of c#. recently I got a problem to save a image (jpg format) from a picturebox to database. I am using sql database. and visual studio 2008. i can show any image to picture box in my window application. but could not save that image in database.

plz if u could solve my problem.

thank you,
Md. Shafiqul Azam
Dhaka,
Bangladesh

推荐答案

您好,我们没有将图像保存在数据库中,而是保存了唯一的图像路径数据库,因此您应该将图像保存在文件夹中,并且将其路径保存在数据库,有关更多信息,您可以访问此URL ... 在SQL Server中存储或保存图像 [ ^ ]
Hi we do not save image in database , we save the only image path database, so you should be save your image in a folder, and its path in database, and for more information you can visit this url...Store or Save images in SQL Server[^]


声明此变量:
----------------------

Byte [] b = null;
FileStream Fs = default(FileStream);


单击pictureBox1,然后编写此编码:
---------------------------------------------

Declare this variable:
----------------------

Byte[] b = null;
FileStream Fs = default(FileStream);


click the pictureBox1 then write this coding:
---------------------------------------------

private void PictureBox1_Click(object sender, EventArgs e)
       {
           try
           {
               OpenFileDialog dialog = new OpenFileDialog();

               dialog.Filter = "JPEG(*.jpeg)|*.jpeg|GIF(*.gif)|*.gif|PNG(*.Png)|*.png|AllFiles|*.*";
               if (dialog.ShowDialog() == DialogResult.OK)
               {
                   Fs = File.Open(dialog.FileName, FileMode.OpenOrCreate);
                   b = new Byte[Fs.Length];
                   Fs.Read(b, 0, b.Length);
                   Fs.Close();
                   IMG.Image = Image.FromFile(dialog.FileName);
               }
               else
               {
                   MessageBox.Show("Select Image", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }




点击保存按钮:
-----------

SqlCmd.Parameters.AddWithValue("@ img",b);




Click save Button:
-----------

SqlCmd.Parameters.AddWithValue("@img", b);


这篇关于如何将图片从图片盒保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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