Victor.Stone
dot net 备忘录
posts - 21,  comments - 25,  trackbacks - 0

namespace ImageTest
{
    
public partial class Form1 : Form
    
{

        
private BindingSource bindsrc;
        
private DataTable tbl;
        
private string strConnectSqlServer;
        
private ImageFormat currentFormat;

        
public Form1() 
        
{
            InitializeComponent();
            
this.sPhotoData.SizeMode = PictureBoxSizeMode.StretchImage;
            
this.bindsrc = new BindingSource();
            
this.bindsrc.CurrentItemChanged += new EventHandler(bindsrc_CurrentItemChanged);


            
//设置SqlServer连接串
            SqlConnectionStringBuilder strBuilder = new SqlConnectionStringBuilder();
            strBuilder.DataSource 
= "127.0.0.1";
            strBuilder.InitialCatalog 
= "mail";
            strBuilder.IntegratedSecurity 
= true;
            
this.strConnectSqlServer = strBuilder.ToString();

            
this.btnReresh_Click(nullnull);
            
this.btnOpen.Enabled = false;

        }


        
private void bindsrc_CurrentItemChanged(object sender, EventArgs e)
        
{
            
//刷新界面显示
            DataRowView rowView = this.bindsrc.Current as DataRowView;
            
if (rowView == nullreturn; }

            
this.sPhotoId.Text = rowView["PhotoId"].ToString();
            
this.sPhotoDesc.Text = rowView["PhotoDesc"].ToString();

            
if (rowView["PhotoData"== null || rowView["PhotoData"== DBNull.Value)
            
{
                
return;
            }


            
using(MemoryStream ms = new MemoryStream((byte[])rowView["PhotoData"]))
            
try
            
{
                
this.sPhotoData.Image = Image.FromStream(ms);
            }

            
finally
            
{
                ms.Close();
            }

        }


        
private void btnReresh_Click(object sender, EventArgs e)
        
{
             
//数据查询
            using (SqlConnection con = new SqlConnection(this.strConnectSqlServer))
            
{
                
string strSelect = "select * from dbo.myPhoto";
                SqlDataAdapter adapter 
= new SqlDataAdapter(strSelect, con);
                
this.tbl = new DataTable();
                adapter.Fill(
this.tbl);
            }


            
//设置界面显示
            this.bindsrc.DataSource = this.tbl;
            
this.dataGridView1.DataSource = this.bindsrc;
        }


        
private void btnAddSave_Click(object sender, EventArgs e)
        
{
            
//将图像读入到字节数组
            
            
byte[] buffByte;
            
using(MemoryStream ms = new MemoryStream())
            
try
            
{
                
this.sPhotoData.Image.Save(ms, this.currentFormat); ;
                
//buffByte = new byte[ms.Length];
                
//ms.Read(buffByte, 0, (int)ms.Length);
                buffByte = ms.GetBuffer();
            }

            
finally
            
{
                ms.Close();
            }


            
//数据保存
            using (SqlConnection con = new SqlConnection(this.strConnectSqlServer))
            
{
                
string strInsert = @"
INSERT INTO dbo.myPhoto(RId, photoId, photoDesc, photoData)
VALUES(NEWID(), @photoId, @photoDesc, @photoData)
";
                SqlCommand cmd 
= new SqlCommand(strInsert, con);
                cmd.Parameters.Add(
"@photoId", SqlDbType.NVarChar);
                cmd.Parameters.Add(
"@photoDesc", SqlDbType.NVarChar);
                cmd.Parameters.Add(
"@photoData", SqlDbType.Image);
                cmd.Parameters[
"@photoId"].Value = this.sPhotoId.Text;
                cmd.Parameters[
"@photoDesc"].Value = this.sPhotoDesc.Text;
                cmd.Parameters[
"@photoData"].Value = buffByte;
                con.Open();
                MessageBox.Show(
"影响" + cmd.ExecuteNonQuery().ToString()+"");
                con.Close();
            }

        }


        
private void btnEditSave_Click(object sender, EventArgs e)
        
{

        }


        
private void btnOpenPicture_Click(object sender, EventArgs e)
        
{
            OpenFileDialog dlg 
= new OpenFileDialog();

            
//确定类型
            if (this.sImageFormat.SelectedIndex <= 0)
            
{
                MessageBox.Show(
"请选择类型""警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
return
            }


            
//JPG  BMP  GIF  PNG
            switch (this.sImageFormat.SelectedItem.ToString())
            
{
                
case "JPG":
                    
this.currentFormat = ImageFormat.Jpeg;
                    
break;
                
case "BMP":
                    
this.currentFormat = ImageFormat.Bmp;
                    
break;
                
case "GIF":
                    
this.currentFormat = ImageFormat.Gif;
                    
break;
                
case "PNG":
                    
this.currentFormat = ImageFormat.Png;
                    
break;
            }


            
if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; }
            
string pathName = dlg.FileName;
            
            
this.sPhotoData.Image = System.Drawing.Image.FromFile(pathName);
        }


        
private void sImageFormat_SelectedIndexChanged(object sender, EventArgs e)
        
{
            
this.btnOpen.Enabled = this.sImageFormat.SelectedIndex > 0;
        }

    }

}



posted on 2012-03-08 00:15 Victor.Stone 阅读(206) 评论(0)  编辑 收藏 引用 所属分类: .net framework
只有注册用户登录后才能发表评论。

<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(2)

随笔分类(17)

文章分类

搜索

  •  

积分与排名

  • 积分 - 23851
  • 排名 - 192

最新随笔

阅读排行榜