如何在Windows窗体应用程序中将图像保存到数据库(sql)中 [英] How to save images into database (sql) in Windows form Application

查看:94
本文介绍了如何在Windows窗体应用程序中将图像保存到数据库(sql)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像存储到数据库中,我知道它只通过字节格式,我不知道这个。



i想要插入和检索的示例代码来自windfroms的sql数据库的图片,它可能吗?



提前致谢!!



问候

AR

i want to image store into database, i know its through only byte format, i have no idea about this.

i want sample code for insert and retrieve the image from sql database in windfroms, its possible ?

Thanks in advance!!

Regards
AR

推荐答案




我能看到一个很好的例子。这可能会对你有所帮助。



http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html [ ^ ]





问候

Dominic
Hi
I could see an excellent example for this. This may help you.

http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html[^]


Regards
Dominic


使用Google或Codeproject搜索,你会得到.......

使用ASP.NET中的SQL Server上载和下载文件 [ ^ ]
Use Google or Codeproject search, you'll get .......
Upload and Download Files with SQL Server in ASP.NET[^]


你好,



您只需从工具箱中取出一个图片框控件。现在通过这种方式设置图片框图像。



Hello ,

you just take one picturebox control from the toolbox . now set the picturebox image by this way.

private void btnBrowser_Click(object sender, EventArgs e)
     {
         openFileDialog1.Title = "Choose Image";
         openFileDialog1.Filter = "Images (*.JPEG;*.BMP;*.JPG;*.GIF;*.PNG;*.)|*.JPEG;*.BMP;*.JPG;*.GIF;*.PNG";
         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             Image img = new Bitmap(openFileDialog1.FileName);
             pictureBoxCompanyLogo.Image = img;// resizeImage(img);
         }
     }





在将此图片保存到数据库之前,将此图片框图像转换为byte [] format。



and before save this image into database , convert this picturebox image into byte[] format .

public static  byte[] ImageToByteArray(Image img,PictureBox pictureBoxCompanyLogo)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (pictureBoxCompanyLogo.Image != null)
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            return ms.ToArray();
        }



并调用此方法


and call this method

byte[] byteImg=ImageToByteArray(pictureBoxCompanyLogo.Image, pictureBoxCompanyLogo);





现在将此byte []图像保存在数据库中(在sqlserver的Image列中)



从Sqlserver检索图像时,将byte []转换为原始格式。





Now save this byte[] image in your database (in the Image column of sqlserver)

while retrieving the image from Sqlserver convert the byte[] into original format by this way.

public Image GetDataToImage(byte[] pData)
        {
            try
            {
                ImageConverter imgConverter = new ImageConverter();
                return imgConverter.ConvertFrom(pData) as Image;
            }
            catch (Exception ex)
            {
                MsgBox.Show(ex.Message, "Error", MsgBox.Buttons.OK, MsgBox.Icon.Error);
                return null;
            }
        }





并最终设置图片框图片由



and ultimately set the picturebox image by

//call "GetDataToImage" this function before load the image in picturebox
 pictureBoxCompanyLogo.Image = GetDataToImage((byte[])byteimage);





谢谢



thanks


这篇关于如何在Windows窗体应用程序中将图像保存到数据库(sql)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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