Monday, November 5, 2012

Images upload and save to folder in C# windows application


 private void btnBrowseimage_Click(object sender, EventArgs e)
        {
            try
            {
             
                btnSubmit.Enabled = false;
                gbLabels.Visible = false;
                gbLabels.BackgroundImage = null;

                OpenFileDialog fDialog = new OpenFileDialog();
                fDialog.AddExtension = true;
                fDialog.CheckFileExists = true;
                fDialog.CheckPathExists = true;
                fDialog.Title = "Open Image Files";
                fDialog.Filter = "All files (*.*)|*.*|JPEG Files|*.jpeg|GIF Files|*.gif";
                fDialog.InitialDirectory = @"C:\";
                fDialog.Multiselect = true;
                if (fDialog.ShowDialog() == DialogResult.OK)
                {

                    imageFiles = fDialog.FileNames;
                    Files = new string[imageFiles.Length];
                    for (int j = 0; j <= imageFiles.Length - 1; j++)
                    {
                        System.Drawing.Image im = System.Drawing.Image.FromFile(imageFiles[j]);
                        int ActualWidth = im.Width;
                        int ActualHeight = im.Height;
                        im.Dispose();
                        if (ActualWidth > 3600 && ActualHeight > 2400)
                        {
                            MessageBox.Show("Please upload image with size less than  3600X2400", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {

                            txtImageName.Text = imageFiles[j];
                            FileStream fssource = new FileStream(txtImageName.Text, FileMode.Open);

                            var root1 = txtImageName.Text;
                            var temp1 = root1.Split('\\');
                            var image = temp1[temp1.Length - 1];

                            var root = Application.ExecutablePath;

                            var temp = root.Split('\\');
                            temp[temp.Length - 1] = "";
                            temp[temp.Length - 2] = "Images";
                            var newpath = string.Join("\\", temp);



                            bool folderExists = Directory.Exists(newpath);
                            if (!folderExists)
                                Directory.CreateDirectory(newpath);


                            newpath = newpath + "_" + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + "ReceiptionBack" + "." + image.Split('.')[1];



                            txtImageName.Text = newpath;
                            Files[j] = txtImageName.Text;

                            FileStream fsdest = new FileStream(newpath, FileMode.Create);
                            while (true)
                            {
                                int i;
                                i = fssource.ReadByte();
                                if (i == -1)
                                    break;
                                fsdest.WriteByte(Convert.ToByte(i));
                            }
                            fsdest.Close();
                        }

                    }

                }
                btnSubmit.Enabled = true;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...