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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:29:22+00:00 2026-06-18T00:29:22+00:00

I’ve got a method which takes an input image, does some things to the

  • 0

I’ve got a method which takes an input image, does some things to the image, then saves it to another file. At its most basic it resizes the image, but it can do a few more complicated things like converting to greyscale, quantizing etc, but for the case of this question I’m only attempting to resize the image and not performing any of the other actions.

It looks something like:

public void SaveImage(string src, string dest, int width, int height, ImageFormat format, bool deleteOriginal, bool quantize, bool convertToGreyscale) {
    // Open the source file
    Bitmap source = (Bitmap)Image.FromFile(src);

    // Check dimensions
    if (source.Width < width)
        throw new Exception();
    if (source.Height < height)
        throw new Exception();

    // Output image
    Bitmap output = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage(output)) {
        // Resize the image to new dimensions
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(source, 0, 0, width, height);
    }

    // Convert to greyscale if supposed to
    if (convertToGreyscale) {
        output = this.ConvertToGreyscale(output);
    }

    // Save the image
    if (quantize) {
        OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
        using (var quantized = quantizer.Quantize(output)) {
            quantized.Save(dest, format);
        }
    }
    else {
        output.Save(dest, format);
    }

    // Close all the images
    output.Dispose();
    source.Dispose();

    // Delete the original
    if (deleteOriginal) {
        File.Delete(src);
    }
}

Then to use it I’d call something like: imageService.SaveImage("c:\image.png", "c:\output.png", 300, 300, ImageFormat.Png, false, false, false);

That should open up the “image.png” file, resize in to 300×300, then save it to “output.png” as a PNG file. But it’s not working—the file that gets created is in the correct location but has a file size of zero and contains no image at all.

This also only seems to happen when I pass in the parameter ImageFormat.Png; if I pass ImageFormat.Jpeg, then it works fine and creates the image file perfectly.

I’m wondering if there’s some sort of delay occurring between creating the image and somewhere else in the code attempting to access the image that’s been created (not in the above code) which locks up the file and so it’s never written to? Could that be the case?

Any ideas what else could be going on?

Cheers

Edits:

  • remove redundant cast pointed out by Lloyd
  • 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-18T00:29:24+00:00Added an answer on June 18, 2026 at 12:29 am

    There are some historical issues with saving bitmaps as png.

    Using System.Windows.Media.Imaging.PngBitmapEncoder can resolve this

    See System.Windows.Media.Imaging.PngBitmapEncoder

    And How to: Encode and Decode a PNG Image for a sample.

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

Sidebar

Related Questions

I've got an ATL method that takes a DATE type, which is really a
I've got a method in a class that's writing to some string, which calls
I've got following problem: (c#) There is some class (IRC bot), which has method,
I've got a function api_request which takes API method as an argument, and returns
I got the following method which is used to read a txt file and
I have got myself quite befuddled. I have some code which takes some images,
I've got a C# method that takes a bunch of parameters, all of which
Got an HTML Select with an onChange() event that calls a method which takes
I've got a method which takes a IList<Person> and returns an IEnumberable as so:
I've got a method which currently takes an IList as a parameter, where UserBO

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.