コード例 #1
0
 private void btnLoadAndSave_Click(object sender, EventArgs e)
 {
     #region Save Image to DB
     SqlConnection con = new SqlConnection(DBHandler.GetConnectionString()); //connection to the your database
     try
     {
         OpenFileDialog fop = new OpenFileDialog();                                                                         //create object of open file dialog
         fop.InitialDirectory = @"C:\";                                                                                     //set Initial directory
         fop.Filter           = "[JPG,JPEG]|*.jpg";                                                                         //set filter for select only .jpg files
         if (fop.ShowDialog() == DialogResult.OK)                                                                           //display open file dialog to user and only user select a image enter to if block
         {                                                                                                                  //@fop.FileName
             ProcesoDeImagenes.GuardaImagen(@fop.FileName);
             loadImageIDs();                                                                                                //call user defined method to load image IDs to combo box
             MessageBox.Show("Image Save Successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); //display save successful message to user
         }
         else
         {
             MessageBox.Show("Please Select a Image to save!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);//display message to force select a image
         }
     }
     catch (Exception ex)                                                                      //catch if any error occur
     {
         MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); //display error message with exception
     }
     finally
     {
         if (con.State == ConnectionState.Open) //check whether connection to database is open or not
         {
             con.Close();                       //if connection is open then only close the connection
         }
     }
     #endregion
 }
コード例 #2
0
 private void btnDisplayImage_Click(object sender, EventArgs e)
 {
     #region Retrieve Image from DB
     if (cmbImageID.SelectedValue != null) //check whether user select image ID or not
     {
         if (picImage.Image != null)       //check whether picture box contain image or not
         {
             picImage.Image.Dispose();     //clear the image of the picture box if there is image
         }
         string _id = cmbImageID.Text;
         picImage.Image    = ProcesoDeImagenes.VisualizarImagen(_id);
         picImage.SizeMode = PictureBoxSizeMode.StretchImage; //set size mode property of the picture box to stretch
         picImage.Refresh();                                  //refresh picture box
     }
     else
     {
         MessageBox.Show("Please Select a Image ID to Display!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);//display message to force select a image ID
     }
     #endregion
 }