Click here to Skip to main content
15,896,118 members
Articles / Web Development / ASP.NET

Resize/Shrink Photos for Web Development

Rate me:
Please Sign up or sign in to vote.
3.91/5 (9 votes)
27 Oct 2008CPOL3 min read 39K   472   43  
Resize/Shrink Photos for Web Development to reduce download time
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ImageShrinker
{
    public partial class Page : UserControl
    {
        double originalWidth = 426, originalHeight = 124; // size of InitialImage.jpg
        private FileInfo currentFile;

        public Page()
        {
            InitializeComponent();
            resizePanel.Width = originalWidth;
            resizePanel.Height = originalHeight;
            slider.Value = 1000;
            displayImageSize();
        }

        private void slider_MouseMove(object sender, MouseEventArgs e)
        {
            UpdateSize();
        }

        private void UpdateSize()
        {
            resizePanel.Width = (slider.Value / 1000) * originalWidth;
            displayImageSize();
        }

        private void displayImageSize()
        {
            string strWidth = resizePanel.Width.ToString("F0");
            string strHeight = ((slider.Value / 1000) * originalHeight).ToString("F0");
            sliderToolTip.Text = String.Format("{0} x {1}", strWidth, strHeight);
            object[] paramArray = new object[1];
            paramArray[0] = sliderToolTip.Text;
            try
            {
                System.Windows.Browser.HtmlPage.Window.Invoke("updateStatus", paramArray);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Failure to call JavaScript function updateStatus(). Did you open the proper page?\nIn Visual Studio, did you set the Web project as StartUp project and set the Start Page?", "Wrong page", MessageBoxButton.OK);
            }
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Image Files (*.png;*.jpg)|*.png;*.jpg|All files (*.*)|*.*";
            ofd.FilterIndex = 1;
            ofd.Multiselect = false;

            if (ofd.ShowDialog() == true)
            {
                currentFile = ofd.File;
                int fileSize = Convert.ToInt32(currentFile.Length);
                tbxFileName.Text = currentFile.Name;
                btnSave.IsEnabled = true;

                using (FileStream stream = currentFile.OpenRead())
                {
                    byte[] buffer = new byte[currentFile.Length];
                    stream.Read(buffer, 0, (int)currentFile.Length);
                    string imageData = Convert.ToBase64String(buffer);
                    WebClient wc = new WebClient();
                    wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
                    wc.UploadStringAsync(new Uri("getsize.ashx", UriKind.Relative), imageData);

                    resizePanel.Visibility = Visibility.Collapsed;
                    BitmapImage bi = new BitmapImage();
                    bi.SetSource(stream);
                    image.Source = bi;
                    slider.Value = 1000;
                }
            }
        }

        void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            string size = e.Result;
            string[] part = size.Split(new char[] { ',' });
            resizePanel.Width = Convert.ToInt32(part[0]);
            resizePanel.Height = Convert.ToInt32(part[1]);
            resizePanel.Visibility = Visibility.Visible;
            originalWidth = resizePanel.Width;
            originalHeight = resizePanel.Height;
            displayImageSize();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            double width = image.ActualWidth;

            string imageData = null;
            using (FileStream stream = currentFile.OpenRead())
            {
                byte[] buffer = new byte[currentFile.Length];
                stream.Read(buffer, 0, (int)currentFile.Length);
                imageData = Convert.ToBase64String(buffer);
            }

            object[] paramArray = new object[3];
            paramArray[0] = imageData;
            paramArray[1] = slider.Value / 1000;
            paramArray[2] = tbxFileName.Text.Trim();
            System.Windows.Browser.HtmlPage.Window.Invoke("echo", paramArray);
        }

        private void slider_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Left:
                    if (slider.Value > 0)
                    {
                        slider.Value = slider.Value - 1;
                    }
                    break;
                case Key.Right:
                    if (slider.Value < 1000)
                    {
                        slider.Value = slider.Value + 1;
                    }
                    break;
            }
            UpdateSize();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Marc Schluper studied Applied Mathematics at Technical University Eindhoven, The Netherlands.
His employer is Kronos Hiring Solutions in Beaverton, OR.
He is married and has two children.

Comments and Discussions