private void SavePicture() { DialogResult result = openFileDialog1.ShowDialog(); if (result != DialogResult.OK) { return; } string ext = Path.GetExtension(openFileDialog1.FileName).ToLower(); if (ext != ".jpg") { MessageBox.Show("對不起!只接受jpg檔"); return; } int recipeID = CurrentPhotoID(); if (recipeID < 0) { MessageBox.Show("無法取得本筆配方 RecipeID , 無法繼續存檔!"); return; } var photos = from r in m_DataSet.Photos where r.PhotoID == recipeID && r.TableID == (short)PhotoTableID.Recipe select r; MyDataSet.PhotosRow photo = null; if (photos.Count() > 0) { photo = photos.First(); } photo = SavePhotoFileToDB(openFileDialog1.FileName, recipeID, (short)PhotoTableID.Recipe, 384, 256, photo); ShowPhotoDB(photo); }
void ShowPhotoDB(MyDataSet.PhotosRow row) { if (row == null || row.IsPhotoNull()) { return; } MemoryStream stream = new MemoryStream(row.Photo); Image bmp = Image.FromStream(stream); photoPictureBox.Image = bmp; }
private void SavePicture() { DataRowView rowView = productBindingSource.Current as DataRowView; var row = rowView.Row as MyProductRow; if (row.ProductID <= 0) { MessageBox.Show("抱歉! 新增時內碼小於 0 無法存照片!\r\n請存檔後關閉產品表,再重新進入設定照片!"); return; } DialogResult result = openFileDialog1.ShowDialog(); if (result != DialogResult.OK) { return; } string ext = Path.GetExtension(openFileDialog1.FileName).ToLower(); if (ext != ".jpg") { MessageBox.Show("對不起!只接受jpg檔"); return; } int productID = CurrentPhotoID(); if (productID < 0) { MessageBox.Show("無法取得本筆產品 ProductID , 無法繼續存檔!"); return; } var photos = from r in m_DataSet.Photos where r.PhotoID == productID && r.TableID == (int)PhotoTableID.Product select r; MyDataSet.PhotosRow photo = null; if (photos.Count() > 0) { photo = photos.First(); } photo = SavePhotoFileToDB(openFileDialog1.FileName, productID, (short)PhotoTableID.Product, 240, 160, photo); ShowPhotoDB(photo); }
private MyDataSet.PhotosRow SavePhotoFileToDB(string fileName, int id, short tableID, int width, int height, MyDataSet.PhotosRow photo) // photo==null 就新增 { Cursor = Cursors.WaitCursor; MD5 MD5Provider = new MD5CryptoServiceProvider(); try { Bitmap img = (Bitmap)(Bitmap.FromFile(fileName)); Bitmap shrank = MyFunction.ShrinkBitmap(img, width, height); // 使用SQLServer時,只存縮圖以節省網路傳輸時間, 產品統一尺寸 W240 H160 MemoryStream stream = new MemoryStream(); shrank.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); if (photo == null) { photo = m_DataSet.Photos.NewPhotosRow(); photo.TableID = tableID; photo.PhotoID = id; photo.Photo = stream.ToArray(); photo.UpdatedTime = DateTime.Now; photo.MD5 = MD5Provider.ComputeHash(photo.Photo); m_DataSet.Photos.AddPhotosRow(photo); } else { photo.Photo = stream.ToArray(); photo.UpdatedTime = DateTime.Now; photo.MD5 = MD5Provider.ComputeHash(photo.Photo); } PhotoAdapter.Update(m_DataSet.Photos); } catch (Exception ex) { MessageBox.Show("存配方照片<" + id.ToString() + ">時出錯!原因:" + ex.Message); } Cursor = Cursors.Arrow; return(photo); }