Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:47:45+00:00 2026-06-17T15:47:45+00:00

I am uploading a jpeg image and storing it in a database as a

  • 0

I am uploading a jpeg image and storing it in a database as a byte array. However when i retrieve the image and select ‘save as’ the image is now a PNG and the file size is 60-70% larger. Here is the function which does the converting. Can anybody suggest why this is happening?

private byte[] ResizeImage(UploadedFile file, int maxWidth, int maxHeight)
{
    int canvasWidth = maxWidth;
    int canvasHeight = maxHeight;

    using (Bitmap originalImage = new Bitmap(file.InputStream))
    {
        int originalWidth = originalImage.Width;
        int originalHeight = originalImage.Height;

        Bitmap thumbnail = new Bitmap(canvasWidth, canvasHeight); // create thumbnail canvas

        using (Graphics g = Graphics.FromImage((System.Drawing.Image)thumbnail))
        {
            g.SmoothingMode = SmoothingMode.Default;
            g.InterpolationMode = InterpolationMode.Default;
            g.PixelOffsetMode = PixelOffsetMode.Default;



            //Get the ratio of original image     
            double ratioX = (double)canvasWidth / (double)originalWidth;
            double ratioY = (double)canvasHeight / (double)originalHeight;
            double ratio = ratioX < ratioY ? ratioX : ratioY; // use which ever multiplier is smaller 

            // Calculate new height and width     
            int newHeight = Convert.ToInt32(originalHeight * ratio);
            int newWidth = Convert.ToInt32(originalWidth * ratio);

            // Calculate the X,Y position of the upper-left corner      
            // (one of these will always be zero)     
            int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
            int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);

            //g.Clear(Color.White); // Add white padding

            g.DrawImage(originalImage, posX, posY, newWidth, newHeight);

        }

        /* Display uploaded image preview */
        ImageConverter converter = new ImageConverter();
        byte[] imageData = (byte[])converter.ConvertTo(thumbnail, typeof(byte[])); // Convert thumbnail into byte array and set new binary image

        return imageData;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:47:47+00:00Added an answer on June 17, 2026 at 3:47 pm

    I’m not sure whether ImageConverter supports other formats, so try

    byte[] imageData;
    using (MemoryStream stream = new MemoryStream())
    {
        thumbnail.Save(stream, ImageFormat.Jpeg);
        imageData = stream.ToArray();
    }
    return byteArray;
    

    If you want more control (e.g. specify the compression level), check this out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am uploading a image file to the server and resizing it to save
i have a problem with uploading. if ((($_FILES[file][type] == image/gif) || ($_FILES[file][type] == image/jpeg)
Possible Duplicate: Image uploading via PHP returning empty array I am unable to successfully
I am uploading an image to an asmx web service. The file upload works
When uploading a file (jpeg) via a form in IE7 I am seeing a
I'm trying to change the file name at the point of uploading. This image
hey guys im wondering on how to rename a file specially image before uploading
While uploading a file bigger than post_max_size in Symfony, the uploaded file is allocated
I'm having problems uploading files with CodeIgniter 2.1.0, as I recieve the $_FILES array
am I uploading this file right? Here is my code: Deleted Code it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.