Friday, 4 April 2014

Re-sized Image of byte[] array type in asp.net

I found the solutions of image re-sizing at run-time in asp.net 
use this code to obtain re-sized image:

SqlCommand cmd = new SqlCommand("select ProductPicture from ProductDetails where Id=" + pdctid + "", con);
                byte[] product = (byte[])cmd.ExecuteScalar();
                MemoryStream stream = new MemoryStream(product);
                Response.BinaryWrite(ResizeUploadedImage(stream));


private byte[] ResizeUploadedImage(Stream streamToResize)
        {
            byte[] resizedImage;
            using (System.Drawing.Image orginalImage = System.Drawing.Image.FromStream(streamToResize))
            {
                ImageFormat orginalImageFormat = orginalImage.RawFormat;
                int orginalImageWidth = orginalImage.Width;
                int orginalImageHeight = orginalImage.Height;
                int resizedImageWidth = 150; // Type here the width you want
                int resizedImageHeight = 90; // Type here the hight you want
                using (Bitmap bitmapResized = new Bitmap(orginalImage, resizedImageWidth, resizedImageHeight))
                {
                    using (MemoryStream streamResized = new MemoryStream())
                    {
                        bitmapResized.Save(streamResized, orginalImageFormat);
                        resizedImage = streamResized.ToArray();
                    }
                }
            }

            return resizedImage;
        }