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 1016297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:31:43+00:00 2026-05-16T10:31:43+00:00

I am loading images from a database and want to dynamically resize them according

  • 0

I am loading images from a database and want to dynamically resize them according to some input.

Code is something like this:

public ActionResult GetImage(string imageID, int? width, int? height, bool constrain)
    {
        ValidateImageInput(width, height, constrain);
        ImageWithMimeType info = LoadFromDatabase(imageID);

        if(info == null)
            throw new HttpException(404, "Image with that name or id was not found.");

        Resize(info.Bytedata, width, height, constrain, info.MimeType);

        return File(info.Data, info.MimeType);
    }

How would I implement Resize in a way that preserves encoding type etc? I’ve looked at Image resizing efficiency in C# and .NET 3.5 but don’t see how it would preserve encoding – since creating a new Bitmap surely isn’t encoded?

  • 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-05-16T10:31:43+00:00Added an answer on May 16, 2026 at 10:31 am

    Fact is, I managed to solve it with some help of google eventually. Guess I was a bit too trigger happy with the question. Anyway, the basic bits is that I look up the proper ImageFormat from the mimetype using ImageCodecInfo.GetImageEncoders(), then save using the correct encoding, as following:

        private ImageFormat GetEncoderInfo(string mimeType)
        {
            // Get image codecs for all image formats
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
    
            // Find the correct image codec
            for (int i = 0; i < codecs.Length; i++)
                if (codecs[i].MimeType == mimeType)
                    return new ImageFormat(codecs[i].FormatID);
            return null;
        }
    

    This is a slightly different version I made of the code on http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

    Using the ImageFormat I can simply do

    image.Save(dest, GetEncoderInfo(mimetype));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to stop images from loading, as in not even get a
I am loading images from a single PostgreSQL database shared currently by my production
I have a situation where I'm lazy loading images from the www. It's a
is there a way with javascript/jquery to prevent images from loading? I am building
I'm loading an image from a file, and I want to know how to
I am using the following code to get data from the database( from cs
i have some images that get displayed on a page, some of them don't
I'm pulling some info from a database then putting it into a DIV and
So I'm loading images from a web service, but the size of the images
I am loading images of size 1800x1300 in UIImage.(getting the images from server..)then i

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.