c#学习笔记(二):保存图片、保存DataGridView数据到本地和从本地读取到DataGridView

1、保存图片

    private void saveBtn_Click(object sender, System.EventArgs e)  
        {  
           bool isSave = true;  
           SaveFileDialog saveImageDialog = new SaveFileDialog();  
           saveImageDialog.Title = "图片保存";  
           saveImageDialog.Filter= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";  
 
            if(saveImageDialog.ShowDialog() == DialogResult.OK)  
           {  
              string fileName = saveImageDialog.FileName.ToString();  
     
               if(fileName != "" && fileName != null)  
                {  
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".")+1).ToString();  
 
                   System.Drawing.Imaging.ImageFormat imgformat = null;       
 
                    if(fileExtName!="")  
                    {  
                       switch(fileExtName)   
                        {   
                           case "jpg":   
                               imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;   
                                break;   
                            case "bmp":   
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;   
                                break;   
                           case "gif":   
                               imgformat = System.Drawing.Imaging.ImageFormat.Gif;   
                               break;   
                           default:   
                                MessageBox.Show("只能存取为: jpg,bmp,gif 格式");   
                                isSave = false;  
                               break;   
                        }   
 
                    }  
 
                   //默认保存为JPG格式  
                   if(imgformat == null)  
                    {  
                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;  
                   }  
                      
                   if(isSave)  
                   {  
                      try  
                       {
<span style="white-space:pre">			</span>//此处等等picture_QRCode是图片控件的name
                           this.picture_QRCode.Image.Save(fileName, imgformat);  
                                //MessageBox.Show("图片已经成功保存!");  
                       }  
                        catch  
                       {  
                            MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");  
                     }  
                   }  
 
               }  

           }  
      }
2、保存DataGridView数据到本地

  private void saveData_Click(object sender, EventArgs e)
         { 
            //实例化一个保存文件对话框
            SaveFileDialog sf = new SaveFileDialog();
            //设置文件保存类型
            sf.Filter = "txt文件|*.txt";
            //如果用户没有输入扩展名,自动追加后缀
            sf.AddExtension = true;
            //设置标题
            sf.Title = "写文件";
            //如果用户点击了保存按钮
            if (sf.ShowDialog() == DialogResult.OK)
            {
                //实例化一个文件流--->与写入文件相关联
                FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                //实例化一个StreamWriter-->与fs相关联
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                if (this.dataGridView1.Rows.Count < 1)
                {
                    MessageBox.Show("没有数据!导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    //因为DataGridView最后一行为空,所以减一
                    sw.WriteLine(this.dataGridView1.Rows.Count-1);
                    for (int i = 0; i < this.dataGridView1.Rows.Count-1; i++)
                    {
                        //如果某一列数据为空,就写入"",因为空对象不能调用tostring();
                        if (this.dataGridView1.Rows[i].Cells[0].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[1].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[1].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[2].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[2].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[3].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[3].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[4].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[4].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[5].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[5].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[6].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[6].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[7].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[7].Value.ToString());
                        else
                            sw.WriteLine("");
                        if (this.dataGridView1.Rows[i].Cells[8].Value != null)
                            sw.WriteLine(this.dataGridView1.Rows[i].Cells[8].Value.ToString());
                        else
                            sw.WriteLine("");           
                    }
                    //写入性别
                    sw.WriteLine(sex);
                    sw.WriteLine(age);
                    sw.WriteLine(season);
                    sw.WriteLine(CatCode);
                    //写入商品价格
                    sw.WriteLine(textBox_goodPrice.Text);
                    sw.WriteLine(textBox_goodSaleVolume.Text);
                    sw.WriteLine(textBox_goodName.Text);
                    sw.WriteLine(textBox_goodTag.Text);
                    sw.WriteLine(textBox_goodLink.Text);
                    sw.WriteLine(textBox_goodPicLink.Text);
                    
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
         }

3、读取本地文件的数据到DataGridView

 //加载本地数据到表格
        private void loadData_Click(object sender, EventArgs e)
         {

             //实例化一个保存文件对话框
             OpenFileDialog sf = new OpenFileDialog();
             //设置文件保存类型
             sf.Filter = "txt文件|*.txt";
             //如果用户没有输入扩展名,自动追加后缀
             sf.AddExtension = true;
             //设置标题
             sf.Title = "读文件";
             //如果用户点击了保存按钮
             if (sf.ShowDialog() == DialogResult.OK)
             {
                 //实例化一个文件流--->与写入文件相关联
                 FileStream fs = new FileStream(sf.FileName, FileMode.Open);
                 //实例化一个StreamWriter-->与fs相关联
                 StreamReader sw = new StreamReader(fs);
                 //开始读取
                 if (this.dataGridView1.Rows.Count < 1)
                 {
                     MessageBox.Show("没有数据!读取失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                 }
                 else
                 {
                     int RowCount = int.Parse(sw.ReadLine());
                     //判断如果大于读取的行数小于等于当前dataGridView的行数,就不add行,否则add行
                     if (RowCount < this.dataGridView1.Rows.Count || RowCount == this.dataGridView1.Rows.Count)
                     {
                         for (int i = 0; i < RowCount; i++)
                         {     
                             this.dataGridView1.Rows[i].Cells[0].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[1].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[2].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[3].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[4].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[5].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[6].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[7].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[8].Value = sw.ReadLine();
                         }
                     }
                     else
                     {
                         for (int i = 0; i < RowCount; i++)
                         {
                             this.dataGridView1.Rows.Add();
                             this.dataGridView1.Rows[i].Cells[0].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[1].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[2].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[3].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[4].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[5].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[6].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[7].Value = sw.ReadLine();
                             this.dataGridView1.Rows[i].Cells[8].Value = sw.ReadLine();
                         }
                        }
                    //读取性别Sex
                     string sexRead = sw.ReadLine();
                     switch(sexRead)
                     {
                         case "1":
                             radioSex_m.Checked=true;
                             break;
                         case "2":
                             radioSex_f.Checked=true;
                             break;
                         case "0":
                             radioSex_n.Checked=true;
                             break;
                     }
                     //读取年龄
                     string ageRead = sw.ReadLine();
                      switch(ageRead)
                     {
                         case "1":
                             radioAge_1.Checked=true;
                             break;
                         case "2":
                             radioAge_2.Checked=true;
                             break;
                         case "3":
                             radioAge_3.Checked=true;
                             break;
                     }
                     //读取季节
                     string seasonRead = sw.ReadLine();
                      switch(seasonRead)
                     {
                         case "1":
                             radioSeason_s.Checked=true;
                             break;
                         case "2":
                             radioSeason_w.Checked=true;
                             break;
                         case "3":
                             radioSeason_a.Checked=true;
                             break;
                     }
                     //读取分类
                     string CatCodeRead = sw.ReadLine();
                      switch(CatCodeRead)
                     {
                         case "1":
                             radioCat_1.Checked=true;
                             break;
                         case "2":
                             radioCat_2.Checked=true;
                             break;
                         case "11":
                             radioCat_11.Checked=true;
                             break;
                         case "12":
                             radioCat_12.Checked=true;
                             break;
                         case "13":
                             radioCat_13.Checked=true;
                             break;
                         case "14":
                             radioCat_14.Checked=true;
                             break;
                     }
                     //读取商品价格
                    textBox_goodPrice.Text = sw.ReadLine();                   
                     //读取商品销量
                    textBox_goodSaleVolume.Text = sw.ReadLine();
                     //读取商品名称
                    textBox_goodName.Text = sw.ReadLine();
                     //读取商品标签
                    textBox_goodTag.Text = sw.ReadLine();
                     //读取商品链接
                    textBox_goodLink.Text = sw.ReadLine();
                     //读取图片链接
                    textBox_goodPicLink.Text = sw.ReadLine();


                     //sw.Write(this.textBox1.Text);
                     //清空缓冲区
                   //  sw.Flush();
                     //关闭流
                     sw.Close();
                     fs.Close();
                     MessageBox.Show("读取成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 }
             }
         }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值