Bitmap Save in .Net is saving the image in an incorrect format?
.net, bitmap, c#, jpeg, png
Solution
I'd approach this by:
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.
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?
Problem
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; } } ```