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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:08:04+00:00 2026-05-12T15:08:04+00:00

We have a piece of code which saves a .Net System.Drawing.Bitmap to file. The

  • 0

We have a piece of code which saves a .Net System.Drawing.Bitmap to file. The Save call is specifying the file location as well as the ImageFormat which we are expecting to save the image data as a Jpeg so out code looks like this:

public MediaFile IngestImage(System.Drawing.Bitmap imgSrc, string name){
     ... // left out because it is not relevant to this question
     imgSrc.Save(fullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
     ... // left out because it is not relevant to this question
}

For some reason every now and then this method generates PNG images as .jpg files. Most of the time it is not a big deal however another piece of the project has issues with these files not being actual jpegs (Windows Media Services).

Any help is appreciated, has anyone ever seen this?

Note:
full path is something like “\servcer\share\file.jpg”. We are saving jpg’s with the extension “jpg”. Hence the issue… Later we are creating publishing points on a Windows Media Server to play a SMIL playlist we then have to “Announce” the files and formats to the publishing point when the publishing point starts playing it expects a Jpg file because that is the extension of the file and the content is actually a PNG

Here is the actual code creating the BitpMap object that is passed into the above method…

        public static Bitmap CreateBitmap(string text, int height, int width, Color foregroundColor, Color backgroundColor, string fontName, int fontSize, bool antialias)
    {
        // Initialize graphics
        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            if (antialias)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
            }

            // Set colors
            SolidBrush fgBrush = new SolidBrush(foregroundColor);
            SolidBrush bgBrush = new SolidBrush(backgroundColor);

            // paint background
            RectangleF rectF = new RectangleF(0, 0, width, height);
            g.FillRectangle(bgBrush, rectF);

            // Load font
            FontFamily fontFamily = FontFamily.GenericSerif;
            Font font = new Font(fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
            try
            {
                fontFamily = new FontFamily(fontName);
                font = new Font(fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
            }
            catch { }

            // Set font direction & alignment
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // Finally, draw the text
            g.DrawString(text, font, fgBrush, rectF, format);

            return bmp;
        }
    }
  • 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-12T15:08:04+00:00Added an answer on May 12, 2026 at 3:08 pm

    I’d approach this by:

    1. paring down CreateBitmap(). For instance, does the problem appear if you remove everything except line that calls new and the return line? I’d assume it would, but it’s worth checking. If on the odd chance the problem disappears with this modification, then use a binary search to see if you can track it down to a particular line.

    2. wrapping the Save call with try/catch to see if somewhere a ExternalException is being silently eaten. Bitmap.Save() (inherited from Image.Save()) says the following about ExternalException:

    The image was saved with the wrong
    image format.

    -or-

    The image was saved to the same file
    it was created from.

    Is is possible that the files that fail already exist at fullPath in PNG format with the .jpg extension?

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

Sidebar

Related Questions

I have the following piece of code which replaces template markers such as %POST_TITLE%
I have a piece of code in ANSI C which uses the time.h library
I have a piece of fortran code, and I am not sure which standard
I have written a piece of code which uses the PdfSharp library. The instance
I have this piece of code which gives to SAXParseError i.e., it is not
I have a performance critical piece of code for which I am considering using
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
I have a piece of code looking like this : TAxis *axis = 0;
I have a piece of code here that i really could use some help
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>

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.