I recently came across an interesting set of requirements. I had a collection of images that needed to be re-sized and uploaded to an ftp site. I groaned on the inside remembering implementations featuring tedious height, width, resolution and other calculations. Fortunately, a cool head and swift fingertips for searching prevailed. I learned about a wonderful library called ImageResizer.Net. Did you know it abstracts the much of the ugliness away from the consumer?

I never wanted to know about any of that graphics jargon and lingo, and not because it’s not important. It’s not important to me for what I am doing. Below you can see how trivial the image re-sizing code becomes. FTP Upload in .NET – You ain’t need no libraries proved that I could get along without a library just using the .net framework for this task.

            var versions = new Dictionary();
            versions.Add("_small", "width=100&height=100");
            versions.Add("_medium", "width=300&height=300");
            versions.Add("_large", "width=700&height=700");

            foreach (var version in versions)
            {
                var request = (FtpWebRequest)WebRequest.Create("ftp://{domain}/{path}/OS_" + version.Key + ".png");
                request.Credentials = new NetworkCredential(username, password);
                request.KeepAlive = false;
                request.UseBinary = true;
                request.Method = WebRequestMethods.Ftp.UploadFile;

                var memoryStream = new MemoryStream();
                ImageBuilder.Current.Build("OS.png", memoryStream, new ResizeSettings(version.Value), false, true);
                using (var writer = new BinaryWriter(request.GetRequestStream()))
                {
                    writer.Write(memoryStream.ToArray());
                }
            }

I can’t recommend simple research like this enough. It will save you time, energy, frustration and prevent resolved bugs from appearing.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s