none
How to resize an image in byte[] ? RRS feed

  • Question

  • I have an image from the FileUpload control.  I converted it into a byte[] format.  Then I saved the image into a database table as a varbinary(MAX) column..  Now I would like to resize the image to a smaller size and then save it to the database as a thumbnail image, also in byte[] format. 
    Here is how I converted the image from the FileUpload control to byte[] format:

    byte[] imageBytes = new byte[fuAddImageItem.PostedFile.InputStream.Length];  
    fuAddImageItem.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length); 

    How can an image that is represented by a byte[] be set to a new smaller size within another byte[]?
    • Moved by Peter RitchieModerator Thursday, November 13, 2008 9:48 PM Question not specific to C# language syntax (Moved from Visual C# Language to Visual C# General)
    • Edited by Gary Frank Saturday, November 15, 2008 4:26 PM
    Thursday, November 13, 2008 9:39 PM

Answers

  • This code creates a byte[] that contains an image into a byte[] that contains an image of a different size.  At this point it does create a byte array of the proper size, and it inserts a new row into the table.   I created the code that reads it back out of the table and displays it on the ASP.NET page.  The image displays as it should, which verifies that it is properly encoded in the new byte array. 


            // Create a thumbnail in byte array format from the image encoded in the passed byte array.  
            // (RESIZE an image in a byte[] variable.)  
            public static byte[] CreateThumbnail(byte[] PassedImage, int LargestSide)  
            {  
                byte[] ReturnedThumbnail;  
                  
                using (MemoryStream StartMemoryStream = new MemoryStream(),  
                                    NewMemoryStream = new MemoryStream())  
                {  
                    // write the string to the stream  
                    StartMemoryStream.Write(PassedImage, 0, PassedImage.Length);  
     
                    // create the start Bitmap from the MemoryStream that contains the image  
                    Bitmap startBitmap = new Bitmap(StartMemoryStream);  
     
                    // set thumbnail height and width proportional to the original image.  
                    int newHeight;  
                    int newWidth;  
                    double HW_ratio;  
                    if (startBitmap.Height > startBitmap.Width)  
                    {  
                        newHeight = LargestSide;  
                        HW_ratio = (double)((double)LargestSide / (double)startBitmap.Height);  
                        newWidth = (int)(HW_ratio * (double)startBitmap.Width);  
                    }  
                    else 
                    {  
                        newWidth = LargestSide;  
                        HW_ratio = (double)((double)LargestSide / (double)startBitmap.Width);  
                        newHeight = (int)(HW_ratio * (double)startBitmap.Height);  
                    }  
     
                    // create a new Bitmap with dimensions for the thumbnail.  
                    Bitmap newBitmap = new Bitmap(newWidth, newHeight);  
     
                    // Copy the image from the START Bitmap into the NEW Bitmap.  
                    // This will create a thumnail size of the same image.  
                    newBitmap = ResizeImage(startBitmap, newWidth, newHeight);  
                      
                    // Save this image to the specified stream in the specified format.  
                    newBitmap.Save(NewMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
     
                    // Fill the byte[] for the thumbnail from the new MemoryStream.  
                    ReturnedThumbnail = NewMemoryStream.ToArray();  
                }  
     
                // return the resized image as a string of bytes.  
                return ReturnedThumbnail;  
            }  
     
            // Resize a Bitmap  
            private static Bitmap ResizeImage(Bitmap image, int width, int height)  
            {  
                Bitmap resizedImage = new Bitmap(width, height);  
                using (Graphics gfx = Graphics.FromImage(resizedImage))  
                {  
                    gfx.DrawImage(image, new Rectangle(0, 0, width, height),   
                        new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);  
                }  
                return resizedImage;  
            } 
    • Marked as answer by Gary Frank Saturday, November 15, 2008 3:59 PM
    • Edited by Gary Frank Monday, November 17, 2008 8:06 PM Verified that the code works.
    Friday, November 14, 2008 4:00 PM
  • To scale the image you need very powerful calculations to be performed.  You'll need to first createa Bitmap object from the data and re-draw the image with a new size.  For example:
    Bitmap startBitmap = CreateBitmapFromBytes(imageBytes); // write CreateBitmapFromBytes  
    Bitmap newBitmap = new Bitmap(newWidth, newHeight);     
    using(Graphics graphics = Graphics.FromImage(newBitmap))     
    {     
      graphics.DrawImage(startBitmap, new Rectangle(0,0, newWidth, newHeight), new Rectangle(0,0, startBitmap.Width, startBitmap.Height), GraphicsUnit.Pixel);     
    }     
        
    byte[] newBytes = CreateBytesFromBitmap(newBitmap); // write CreateBytesFromBitmap 

    http://www.peterRitchie.com/blog
    Thursday, November 13, 2008 9:47 PM
    Moderator

All replies

  • To scale the image you need very powerful calculations to be performed.  You'll need to first createa Bitmap object from the data and re-draw the image with a new size.  For example:
    Bitmap startBitmap = CreateBitmapFromBytes(imageBytes); // write CreateBitmapFromBytes  
    Bitmap newBitmap = new Bitmap(newWidth, newHeight);     
    using(Graphics graphics = Graphics.FromImage(newBitmap))     
    {     
      graphics.DrawImage(startBitmap, new Rectangle(0,0, newWidth, newHeight), new Rectangle(0,0, startBitmap.Width, startBitmap.Height), GraphicsUnit.Pixel);     
    }     
        
    byte[] newBytes = CreateBytesFromBitmap(newBitmap); // write CreateBytesFromBitmap 

    http://www.peterRitchie.com/blog
    Thursday, November 13, 2008 9:47 PM
    Moderator
  • Wow Peter, I can't thank you enough!  Your reply appears to have the major ingredients necessary to create a thumbnail image at any size desired in byte[] format.  I am going to give this a try right way, then I'll post the results.

    Fabulous, thanks for the quick reply!
    • Edited by Gary Frank Saturday, November 15, 2008 3:58 PM
    Friday, November 14, 2008 12:31 AM
  • This code creates a byte[] that contains an image into a byte[] that contains an image of a different size.  At this point it does create a byte array of the proper size, and it inserts a new row into the table.   I created the code that reads it back out of the table and displays it on the ASP.NET page.  The image displays as it should, which verifies that it is properly encoded in the new byte array. 


            // Create a thumbnail in byte array format from the image encoded in the passed byte array.  
            // (RESIZE an image in a byte[] variable.)  
            public static byte[] CreateThumbnail(byte[] PassedImage, int LargestSide)  
            {  
                byte[] ReturnedThumbnail;  
                  
                using (MemoryStream StartMemoryStream = new MemoryStream(),  
                                    NewMemoryStream = new MemoryStream())  
                {  
                    // write the string to the stream  
                    StartMemoryStream.Write(PassedImage, 0, PassedImage.Length);  
     
                    // create the start Bitmap from the MemoryStream that contains the image  
                    Bitmap startBitmap = new Bitmap(StartMemoryStream);  
     
                    // set thumbnail height and width proportional to the original image.  
                    int newHeight;  
                    int newWidth;  
                    double HW_ratio;  
                    if (startBitmap.Height > startBitmap.Width)  
                    {  
                        newHeight = LargestSide;  
                        HW_ratio = (double)((double)LargestSide / (double)startBitmap.Height);  
                        newWidth = (int)(HW_ratio * (double)startBitmap.Width);  
                    }  
                    else 
                    {  
                        newWidth = LargestSide;  
                        HW_ratio = (double)((double)LargestSide / (double)startBitmap.Width);  
                        newHeight = (int)(HW_ratio * (double)startBitmap.Height);  
                    }  
     
                    // create a new Bitmap with dimensions for the thumbnail.  
                    Bitmap newBitmap = new Bitmap(newWidth, newHeight);  
     
                    // Copy the image from the START Bitmap into the NEW Bitmap.  
                    // This will create a thumnail size of the same image.  
                    newBitmap = ResizeImage(startBitmap, newWidth, newHeight);  
                      
                    // Save this image to the specified stream in the specified format.  
                    newBitmap.Save(NewMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
     
                    // Fill the byte[] for the thumbnail from the new MemoryStream.  
                    ReturnedThumbnail = NewMemoryStream.ToArray();  
                }  
     
                // return the resized image as a string of bytes.  
                return ReturnedThumbnail;  
            }  
     
            // Resize a Bitmap  
            private static Bitmap ResizeImage(Bitmap image, int width, int height)  
            {  
                Bitmap resizedImage = new Bitmap(width, height);  
                using (Graphics gfx = Graphics.FromImage(resizedImage))  
                {  
                    gfx.DrawImage(image, new Rectangle(0, 0, width, height),   
                        new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);  
                }  
                return resizedImage;  
            } 
    • Marked as answer by Gary Frank Saturday, November 15, 2008 3:59 PM
    • Edited by Gary Frank Monday, November 17, 2008 8:06 PM Verified that the code works.
    Friday, November 14, 2008 4:00 PM
  • Gosh, I love it when you find code that just works!  Thank you Gary Frank!!!!  Hero status in my book.

    cheers
    Saturday, January 30, 2010 3:04 AM
  • This is a best choice to solve my problem. I'm really appreciate you and thank you so much Gary Frank.

    Regard

    Visitih.L

    Sunday, July 18, 2010 11:33 AM
  • Awesome bit of code there!

    Saved me hours!

    I did add a bit to the code so that if the original image doesn't have a side that is larger that LargestSide, this function doesn't increase the size of the image.

    I thought I would add it here...

          // set thumbnail height and width proportional to the original image.  
          int newHeight;
          int newWidth;
          double HW_ratio;
          if (startBitmap.Height > Lar
          // If the original image isn't larger on any side than LargestSide
          //  then don't increase the size of the image.gestSide || startBitmap.Width > LargestSide)
          {
            if (startBitmap.Height > startBitmap.Width)
            {
              newHeight = LargestSide;
              HW_ratio = (double)((double)LargestSide / (double)startBitmap.Height);
              newWidth = (int)(HW_ratio * (double)startBitmap.Width);
            }
            else
            {
              newWidth = LargestSide;
              HW_ratio = (double)((double)LargestSide / (double)startBitmap.Width);
              newHeight = (int)(HW_ratio * (double)startBitmap.Height);
            }
          }
          else
          {
            newHeight = startBitmap.Height;
            newWidth = startBitmap.Width;
          }
    
          // create a new Bitmap with dimensions for the thumbnail.  
          Bitmap newBitmap = new Bitmap(newWidth, newHeight);
    
    Sunday, December 5, 2010 8:00 PM