Sunday, 8 December 2013

c# web upload download pic




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace webload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private WebRequest req;

        //connect
        private void button1_Click(object sender, EventArgs e)
        {
            string url = textBox1.Text;
            try
            {
                req = WebRequest.Create(url);
                textBox1.Enabled = false;
                button2.Enabled = true;
                button3.Enabled = true;
            }
            catch (WebException exp)
            {
                MessageBox.Show(exp.Message, "error");
            }
        }

        //download
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;

            HttpWebResponse httpWebReponse = (HttpWebResponse)req.GetResponse();
            Stream stm = httpWebReponse.GetResponseStream();
            Image m = Image.FromStream(stm);
            pictureBox1.Image = m;
            m.Save("m");
           
        }

        //upload
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;

            openFileDialog1.Filter = "png(*.png)|*.png|jpg(*.jpg)|*.jpg|all files(*.*)|*.*";
            openFileDialog1.Title = "browse picture to upload";


            string imagepath = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imagepath = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
               
                pictureBox1.Image= new Bitmap(openFileDialog1.FileName);
               
                WebClient client = new WebClient();
                client.UploadFile(textBox1.Text, imagepath);
            }
        }
    }
}

No comments:

Post a Comment