Geeks With Blogs

The Life and Times of a Dev Yes, we're really that weird

.net makes some things really easy.  Image manipulation is one of them.  Here's a class that will resize and convert images to and from any of the .net formats.  Be aware, however, that conversion to Icons will not work.  For more information, see the article here:  http://channel9.msdn.com/ShowPost.aspx?PostID=133634.  Enjoy!

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;

namespace WowSoft.Utilities
{
    ///


    /// Processes an image to save images in a new format and/or resize the images.
    ///

    public static class ImageProcessor
    {
        public static Stream ConvertImage(Stream input, System.Drawing.Imaging.ImageFormat format, int newWidth, int newHeight)
        {
            Image image = Image.FromStream(input);
            return ConvertImage(image, format, newWidth, newHeight);
        }
        public static Stream ConvertImage(Stream input, System.Drawing.Imaging.ImageFormat format)
        {
            Image image = Image.FromStream(input);
            return ConvertImage(image, format, image.Width, image.Height);
        }
        public static Stream ConvertImage(Image image, System.Drawing.Imaging.ImageFormat format)
        {
            return ConvertImage(image, format, image.Width, image.Height);
        }
        ///
        /// Converts an image using the specified information.
        ///

        /// The image to convert.
        /// The format of the image.  This is what determines the file format, so bmp, jpg, etc.  Uses the ImageFormat enumeration.
        /// The new width of the image.
        /// The new height of the image.
        /// A MemoryStrem object that can be used to recreate the image or save the image to disk.
        public static MemoryStream ConvertImage(Image image, System.Drawing.Imaging.ImageFormat format, int newWidth, int newHeight)
        {
            if (newWidth <= 0)
            {
                newWidth = image.Width;
            }
            if (newHeight <= 0)
            {
                newHeight = image.Height;
            }
            Image newImage = new Bitmap(newWidth, newHeight, image.PixelFormat);
            Graphics g = Graphics.FromImage(newImage);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle r = new Rectangle(0, 0, newWidth, newHeight);
            g.DrawImage(image, r);
            MemoryStream stream = new MemoryStream();
            newImage.Save(stream, format);
            return stream;
        }
        public static Stream ConvertImage(string fileName, System.Drawing.Imaging.ImageFormat format)
        {
            using (StreamReader reader = new StreamReader(fileName))
            {
                return ConvertImage(reader.BaseStream, format);
            }
        }
        public static Stream ConvertImage(string fileName, System.Drawing.Imaging.ImageFormat format, int newWidth, int newHeight)
        {
            using (StreamReader reader = new StreamReader(fileName))
            {
                return ConvertImage(reader.BaseStream, format,newWidth,newHeight);
            }
        }
    }
}

Posted on Friday, February 17, 2006 10:09 AM Work | Back to top


Comments on this post: Image manipulation with dot net 2.0.

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Robert May | Powered by: GeeksWithBlogs.net