in

Share your IT thoughts...

I N F O R M A T I O N   T E C H N O L O G Y   P O R T A L   O F   E X P E R T S
Latest post Thu, Nov 20 2008 12:21 AM by Shoban. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • Fri, Oct 31 2008 2:08 PM

    • shyju
    • Top 10 Contributor
    • Joined on Sat, Jul 5 2008
    • Beginner
    • Points 225

    ASP.NET Image uploading with Resizing

    I have an aspx page which will upload images to server harddisk from client pc

    But now i need to change my program in such a way that it would allow me to resize the image while uploading.

    Does anyone has any idea on this ? I couldnt not find such properties/methods with Input file server control

    Any one there to guide me ?

     

    • Post Points: 5
  • Sat, Nov 1 2008 12:47 PM In reply to

    • shyju
    • Top 10 Contributor
    • Joined on Sat, Jul 5 2008
    • Beginner
    • Points 225

    Re: ASP.NET Image uploading with Resizing

    I have the code to resize the image read from the input file control.(using objects of Graphics & Bitmap class)

    But How can i do it if the user is specifying a url for the image ?

     

    • Post Points: 20
  • Wed, Nov 5 2008 12:39 PM In reply to

    • NinethSense
    • Top 10 Contributor
    • Joined on Tue, Jul 1 2008
    • India
    • Sr. Developer
    • Points 1,825

    Re: ASP.NET Image uploading with Resizing

    Hi Shyju,

    It is a bit difficult to do the resizing at client side. So let us consider do the process at server side. You can upload the image with fileuploader control and save it to a temp folder. Open that file (with the server path) for processing with System.Drawing and System.Drawing.Imaging.

    Here is a .cs file I used for one of my old image resizing utility: System;

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


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

            private void Form1_Load(object sender, EventArgs e)
            {
               
            }

            protected System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
            {
                int sourceWidth = imgPhoto.Width;
                int sourceHeight = imgPhoto.Height;
                int sourceX = 0;
                int sourceY = 0;
                int destX = 0;
                int destY = 0;

                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;

                nPercentW = ((float)Width / (float)sourceWidth);
                nPercentH = ((float)Height / (float)sourceHeight);
                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentH;
                    destX = System.Convert.ToInt16((Width -
                                  (sourceWidth * nPercent)) / 2);
                }
                else
                {
                    nPercent = nPercentW;
                    destY = System.Convert.ToInt16((Height -
                                  (sourceHeight * nPercent)) / 2);
                }

                int destWidth = (int)(sourceWidth * nPercent);
                int destHeight = (int)(sourceHeight * nPercent);

                Bitmap bmPhoto = new Bitmap(Width, Height,
                                  PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                                 imgPhoto.VerticalResolution);

                Graphics grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.Clear(ColorTranslator.FromHtml("#ffffff"));
                grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                grPhoto.DrawImage(imgPhoto,
                    new Rectangle(destX, destY, destWidth, destHeight),
                    new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                    GraphicsUnit.Pixel);

                grPhoto.Dispose();
                return bmPhoto;
            }

            private void button2_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = openFileDialog1.FileName;
                }
               
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Trim().Length > 0 && textBox2.Text.Trim().Length > 0 && textBox3.Text.Trim().Length > 0)
                {
                    Image img = Image.FromFile(textBox1.Text);
                    try
                    {
                        FixedSize(img, int.Parse(textBox2.Text), int.Parse(textBox3.Text)).Save(System.IO.Path.GetFileNameWithoutExtension(textBox1.Text) + "_01.jpg",ImageFormat.Jpeg);
                    }
                    catch
                    {
                        MessageBox.Show("Invalid entries");
                    }
                }
                else
                {
                    MessageBox.Show("Fill all textboxes");
                }
            }

            private void button3_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Trim().Length > 0 && textBox2.Text.Trim().Length > 0 && textBox3.Text.Trim().Length > 0)
                {
                    Image img = Image.FromFile(textBox1.Text);
                    try
                    {
                        pictureBox1.Image = FixedSize(img, int.Parse(textBox2.Text), int.Parse(textBox3.Text));
                    }
                    catch
                    {
                        MessageBox.Show("Invalid entries");
                    }
                }
                else
                {
                    MessageBox.Show("Fill all textboxes");
                }
            }
        }
    }
     

     

    You need to take care of Image img = Image.FromFile(your uploaded file server URL);. This is a windows application but the usage is almost same for web application.

    • Post Points: 20
  • Thu, Nov 20 2008 12:21 AM In reply to

    • Shoban
    • Top 10 Contributor
    • Joined on Fri, Jul 25 2008
    • Trivandrum
    • Developer
    • Points 565

    Re: ASP.NET Image uploading with Resizing

    Thanks !! It will help me as well :)

    Crankup
    Shoban Kumar
    My tech blog

    • Post Points: 5
Page 1 of 1 (4 items)