Friday 19 October 2012

how to save image in folder in windows c#.net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        string s = string.Empty;
        private void Form2_Load(object sender, EventArgs e)
        {
            s =  "Chrysanthemum.jpg";
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        private void save_Click(object sender, EventArgs e)
        {
            string GetImage = (System.Environment.CurrentDirectory);
            GetImage = GetImage.Substring(0, GetImage.Length - GetImage.StartsWith("bin").ToString().Length - 4);
            GetImage += "Resources\\" + textBox2.Text;
            Image img = Image.FromFile(s);
            img.Save(GetImage, System.Drawing.Imaging.ImageFormat.Jpeg);
            // txtFilename.Text = openDiag.FileName.ToString();
            //    pictureBox1.Image = Image.FromFile(txtFilename.Text.Trim());
            //    string x = pictureBox1.ImageLocation;
            //string ImagePath = Application.StartupPath;
            //pnlCommon.BackgroundImage = Image.FromFile("" + ImagePath + "Resources/" + imagenamePL);
            //Image img = Image.FromFile(TxtDesignImage.Text);
            //img.Save(Application.StartupPath + "\\ItemImage\\" + CurrentFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = Image.FromFile(open.FileName);
                    s = open.FileName;
                    textBox2.Text = open.SafeFileName.ToString();
                }
            }
            catch (Exception)
            {
                throw new ApplicationException("Image loading error....");
            }
        }
        private void RemoveImage()
        {
            string GetImage = (System.Environment.CurrentDirectory);
            GetImage = GetImage.Substring(0, GetImage.Length - GetImage.StartsWith("bin").ToString().Length - 4);
            GetImage += "ItemImage\\" + s;
            System.IO.File.Delete(GetImage);
        }
        private void Display_Click(object sender, EventArgs e)
        {
//Resources FOLDER
            string GetImage = (System.Environment.CurrentDirectory);
            GetImage = GetImage.Substring(0, GetImage.Length - GetImage.StartsWith("bin").ToString().Length - 4);
            GetImage += "\\Resources\\" + s;
            pictureBox1.Image = Image.FromFile(GetImage);
//OTHER FOLDER
string GetImage = (System.Environment.CurrentDirectory);
                    GetImage = GetImage.Substring(0, GetImage.Length - GetImage.StartsWith("bin").ToString().Length - 4);

                    //string[] words = GetImage.Split('.');
                    //GetImage = words[0] + GetUniqueKey() + "." + words[1];

                    string serverpath = "FileUploads\\CardType\\" + FileName;
                    GetImage += serverpath;
                    Image img = Image.FromFile(FileFullPath);
                    img.Save(GetImage, System.Drawing.Imaging.ImageFormat.Jpeg);
                    newCardTYpe.Image = serverpath;
                    ERPManagement.GetInstance.InsertCardType(newCardTYpe);
                    MessageBox.Show("Data saved successfully.", "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    TxtCardTypeName.Focus();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Bitmap.FromFile(s);
        }
    }
}

No comments:

Post a Comment