c# image處理


1.將圖片轉為字節流

 public byte[] SaveImage(String path)//輸入圖片路徑
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //將圖片以文件流的形式進行保存
            BinaryReader br = new BinaryReader(fs);
            byte[] imgBytesIn = br.ReadBytes((int)fs.Length);  //將流讀入到字節數組中
            return imgBytesIn;
        }

2.將圖片上傳到數據庫的Blob字段

private void UpLoadEanPic()
        {
            string picFile = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;//該值確定是否可以選擇多個文件
            dialog.Title = "請選擇需要存儲到" + custCode + "中的EAN圖片";
            dialog.Filter = "圖片文件(*.jpg,*.gif,*.bmp,*.png)|*.jpg;*.gif;*.bmp;*.png";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                 picFile = dialog.FileName;
            }
            else
            {
                return;
            }
            byte[] picBytes = SaveImage(picFile);
            string sqlInsertPic = string.Format(@"update cust_code_info set
ean_pic=:picBytes
where cust_code=:custCode");
            OracleParameter op1 = new OracleParameter("picBytes", OracleType.Blob);
            op1.Value = picBytes;
            OracleParameter op2 = new OracleParameter("custCode", OracleType.VarChar);
            op2.Value = custCode;
            IDataParameter[] parameters = new IDataParameter[]{op1,op2};
            int sqlRe = 0;
            try
            {
                sqlRe = DBManager.DBHelp.Instance().ExecuteSql(sqlInsertPic, parameters);
            }
            catch(Exception ex)
            {
                MessageBox.Show("上傳失敗!"+ex.Message);
                return;
            }
            if (sqlRe == 0)
            {
                MessageBox.Show("上傳失敗!請檢查數據並重試!");
                return;
            }
            else
            {
                MessageBox.Show("上傳成功!");
                btn_query_Click(null, null);
            }
        }

3.將blob字段轉為image,即下載圖片

  private void btn_DownLoadEanPic_Click(object sender, EventArgs e)
        {
            string custCode = dataGridView1.CurrentRow.Cells["客戶料號"].Value.ToString();
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "請選擇文件路徑";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                string sqlGetEanPic = string.Format(@"SELECT
                                                        ean_pic
                                                        FROM
                                                            cust_code_info where cust_code='{0}'", custCode);
                object reader = DBManager.DBHelp.Instance().GetDataSingle(sqlGetEanPic);
                if (reader == Convert.DBNull)
                {
                    MessageBox.Show("未上傳EAN圖片,請檢查!");
                    return;
                }
                byte[] bytes = (byte[])reader;//讀到的內容轉化成字節流
                System.IO.MemoryStream ms = new MemoryStream(bytes);//創建流
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);//從流中創建image對象
                string format = GetImageFormat(img);
                if (string.IsNullOrEmpty(format))
                {
                    MessageBox.Show("文件格式已經損壞,請重新上傳!");
                    return;
                }
                img.Save(foldPath + "\\" + custCode + "圖片" + format);//將image對象保存成圖片存入指定位置
                MessageBox.Show("保存成功!");
            }
            else
            {
                return;
            }
        }

4.獲取image圖片格式

 private string GetImageFormat(Image _img)
        {
            string format;
            if (_img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
            {
                format = ".jpg";
                return format;
            }
            if (_img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
            {
                format = ".gif";
                return format;
            }
            if (_img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
            {
                format = ".png";
                return format;
            }
            if (_img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
            {
                format = ".bmp";
                return format;
            }
            format = string.Empty;
            return format;
        }

不斷積累!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM