Upload multiple files using Silverlight

Standard

Early this year I wrote a post about creating a File Uploader using Silverlight and WCF. And today I got a question from one forum, about to create uploader for multiple files. And here is source code, I am just modified my eariler source code slightly to select multiple file.

OpenFileDialog openFileDialog = new OpenFileDialog();
//Hard coding filter - You can read it from client side too.
openFileDialog.Filter = "Image Files|*.jpg;*.bmp;*.png";
openFileDialog.Multiselect = true;
bool? result = openFileDialog.ShowDialog();
if (result.HasValue && result.Value)
{
    spProgress.Visibility = System.Windows.Visibility.Visible;

    Service1Client client = new Service1Client();
    client.DoUploadCompleted += (o, p) =>
    {
        if (p.Error != null)
        {
            MessageBox.Show(p.Error.Message);
        }
        else
        {
            MessageBox.Show("Done");
        }
    };
    client.DoUploadAsync(CreateUploadData(openFileDialog.Files));
    client.CloseAsync();
}

And here is the helper methods.

private Dictionary<string, byte[]> CreateUploadData(IEnumerable<FileInfo> infos)
{
    var files = new Dictionary<string, byte[]>();
    foreach (var fileInfo in infos)
    {
        files.Add(fileInfo.Name, ReadFileContents(fileInfo.OpenRead()));
    }

    return files;
}

private byte[] ReadFileContents(Stream stream)
{
    using (Stream sr = stream)
    {
        byte[] contents = new byte[sr.Length];
        sr.Read(contents, 0, contents.Length);
        return contents;
    }
}

And here is the change in WCF Service.

[OperationContract]
public void DoUpload(Dictionary<string, byte[]> files)
{
    string folder = @"C:\Uploads";
    foreach (var file in files)
    {
        using (FileStream sw = File.OpenWrite(Path.Combine(folder, file.Key)))
        {
            sw.Write(file.Value, 0, file.Value.Length);
        }
    }
    return;
}

Sometimes if you are uploading too many files with large size, you may get some error like Not Found, maxArrayLength exceeded. You can fix this error by addingin the web.config file.

How to fix Error Code 29506, While installing SQL Management Studio Express on Windows 7 x64

Standard

Today I started installing SQL Server Management Studio express on my Windows 7 x64, and I started installing it, after few seconds, I got an error message like this

This installer has encountered an unexpected error installing this package. This may indicate a problem with this package. The error code is 29506

Error Code 29506, While installing SQL Management Studio Express on Windows 7 x64

Error Code 29506, While installing SQL Management Studio Express on Windows 7 x64

My initial thought was may be it is because of some problem with my downloaded installer. I downloaded it again and still the problem exists. Then I tried it with commandline(cmd), with Run As Administrator option and its worked :)

User Group Meeting – 9th July 2011 – Kochi

Standard

Venue:
Orion India Systems Pvt Ltd, 103, 2nd floor, Tejomaya, Info park SEZ, Kakkanad, Kochin-682030.

Agenda

  1. 09:30 – 09:40 Community updates.
  2. 09:40 – 10:40 C# Test Automation – Praseed Pai
  3. 10:40 – 11:20 Silverlight – Prism – Mahima Radhakrishnan
  4. 11:20 – 11:40 Tea Break (15 min)
  5. 11:40 – 12:30 Sharepoint 2010 Programing – Abraham Peter
  6. 12.30 – 01:00 Ask the experts

Speakers

  1. Abraham Peter is a Senior Software Developer with 6 years of experience. He has been working with Microsoft .NET technologies and recently with SharePoint 2010. His experience spans developing fault tolerant systems, implementing secure coding practices, Watch Dog monitors for networks and also on the web application front.
  2. Praseed Pai is a well known software architect from Kochi,Kerala. His areas of interest include Enterprise software development (using C#/.net) ,Engineering Software development (CAD/CAM/C++), Cross Platform C++ development (Windows/Linux/Mac), Computer Graphics, Computational Finance and Domain specific programming languages.
  3. Mahima Radhakrishnan working as a Software Engineer with Orion India Systems Pvt Ltd. She started her carrier as a QA engineer, and currently working in Silverlight 4.0 technology. .She is Microsoft certified specialist in SQL development.

Click here for more details and click here to register.

Image cropping control in C# for Windows Forms

Standard

This post is also inspired by a SO post – Image Cropping Control for WinForms with Selection Rectangle. I had done a similar implementation using VB.Net long back but I couldn’t remember the implementation. :( Today I tried it again and here is an implementation, which helps to crop an Image using selection rectangle.

namespace dotnetthoughts
{
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class CustomPictureBox : PictureBox
    {
        private Rectangle _rectangle;
        private Point _startingPoint;
        private Point _finishingPoint;
        private bool _isDrawing;

        public CustomPictureBox()
        {
            Cursor = Cursors.Cross;
            _startingPoint = new Point();
            _finishingPoint = new Point();
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                _isDrawing = true;
                _startingPoint = new Point(e.X, e.Y);
            }
            Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left && _isDrawing)
            {
                _finishingPoint = new Point(e.X, e.Y);
                if (e.X > _startingPoint.X)
                {
                    _rectangle = new Rectangle(
                        _startingPoint.X <= e.X ? _startingPoint.X : e.X,
                        _startingPoint.Y <= e.Y ? _startingPoint.Y : e.Y,
                        e.X - _startingPoint.X <= 0 ? _startingPoint.X - e.X : e.X - _startingPoint.X,
                        e.Y - _startingPoint.Y <= 0 ? _startingPoint.Y - e.Y : e.Y - _startingPoint.Y);
                }
                else
                {
                    _rectangle = new Rectangle(
                        e.X <= _startingPoint.X ? e.X : _startingPoint.X,
                        e.Y <= _startingPoint.Y ? e.Y : _startingPoint.Y,
                        _startingPoint.X - e.X <= 0 ? _startingPoint.X - e.X : _startingPoint.X - e.X,
                        _startingPoint.Y - e.Y <= 0 ? e.Y - _startingPoint.Y : _startingPoint.Y - e.Y);
                }
            }
            _isDrawing = false;
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            using (Pen pen = new Pen(Color.Red, 2))
            {
                pen.DashStyle = DashStyle.Dash;
                pe.Graphics.DrawRectangle(pen, _rectangle);
            }
        }

        public void CropImage()
        {
            Bitmap bitmap = new Bitmap(_rectangle.Width, _rectangle.Height);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.DrawImage(this.Image, 0, 0, _rectangle, GraphicsUnit.Pixel);
            }
            Image = bitmap;
            //Removing the rectangle after cropping.
            _rectangle = new Rectangle(0, 0, 0, 0);

        }
    }
}

We can modify this control by adding nice to have features like

  1. Overlay while cropping.
  2. Drag and Re-size the selection Rectangle.

Happy Coding :-)